SearchBarRenderer - Here's how to customize it in Android

IanVinkIanVink CAInsider, University ✭✭✭
edited December 2014 in Xamarin.Forms

For more detailed other options, this is handy: http://stackoverflow.com/questions/25626364/textcolor-of-xamarin-forms-searchbar/27683185#27683185

   [assembly: ExportRenderer(typeof (CustomSearchBar), typeof (CustomSearchBarRenderer))]

  namespace Bahai.Android.Renderers
  {
    public class CustomSearchBarRenderer : SearchBarRenderer
  {
    protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            SearchBar element = (SearchBar) this.Element;
            var native = (global::Android.Widget.SearchView) Control;

            // do whatever you want to the controls here!
            //--------------------------------------------
            // element.BackgroundColor = Color.Transparent;
            // native.SetBackgroundColor(element.BackgroundColor.ToAndroid());
            // native.SetBackgroundColor(Color.White.ToAndroid());

            //The text color of the SearchBar / SearchView 
            AutoCompleteTextView textField = (AutoCompleteTextView)
                (((Control.GetChildAt(0) as ViewGroup)
                    .GetChildAt(2) as ViewGroup)
                    .GetChildAt(1) as ViewGroup)
                    .GetChildAt(0);

            if (textField != null)
                textField.SetTextColor(Color.White.ToAndroid());
        }
    }
  }
  }
Sign In or Register to comment.