Android SearchBar with height less than 65 text cut off

MikeRowley403MikeRowley403 CAMember ✭✭✭
edited January 2016 in Xamarin.Forms

I have found an issue in the Android SearchBar, if the height is less than 65 it doesn't center the text vertically and depending on how small you make the SearchBar height it will cut off the top of the text.

After an hour or two of fighting with a custom renderer I have found some OnLayout changes that fix the problem for me and I wanted to share in case anyone else runs across this issue.

What I found is that the top and height of the AutoCompleteTextView within the SearchBar is not adjusted to match the height of the SearchBar when set is XAML. The following custom renderer code resolves the issue for me:

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);
            var linLayout = (LinearLayout)Control.GetChildAt(0);
            //var searchTextView = (TextView)linLayout.GetChildAt(0);
            //var searchButton = (ImageView)linLayout.GetChildAt(1);

            var searchEditFrame = (LinearLayout)linLayout.GetChildAt(2);
            //var searchMagIcon = (ImageView) searchEditFrame.GetChildAt(0);

            var searchPlate = (LinearLayout)searchEditFrame.GetChildAt(1);
            var searchSrcText = (AutoCompleteTextView)searchPlate.GetChildAt(0);
            searchSrcText.SetHeight(Control.Height);
            searchSrcText.Top = 0;
            //var searchCloseButton = (ImageView) searchPlate.GetChildAt(1);

            //var submitArea = (LinearLayout)searchEditFrame.GetChildAt(2);
            //var searchGoButton = (ImageView) submitArea.GetChildAt(0);
            //var searchVoiceButton = (ImageView) submitArea.GetChildAt(1);
        }

Hopefully this code helps someone along the way. I have commented out but left in the other elements of the SearchBar, maybe someone will find those helpful at some point to.

Thanks
M

Posts

  • PranavSharmaPranavSharma AUMember

    I faced sort of similar issue with Windows Phone 8 project. The search bar was rendering just fine on iOS and Android but not on Windows Phone 8. After spending 1/2 hour debugging I found that setting HeightRequest to 65/70 worked.

Sign In or Register to comment.