Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin - Remove SearchBar Underline in Android

I am trying to remove the black underline under the SearchBar Control on Android. I wrote a CustomRender that I thought would accomplish this, but no luck:

[assembly: ExportRenderer(typeof(NoUnderlineSearchBar), typeof(NoUnderlineSearchBarRenderer))]
namespace XamarinDemo.Droid.CustomRenderers
{
    public class NoUnderlineSearchBarRenderer : SearchBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                this.Control.SetBackgroundColor(Android.Graphics.Color.Argb(0, 0, 0, 0));
            }
        }
    }
}

This seems to work for Entry fields, but not SearchBars. Does anyone know how I can remove the SearchBar underline in a Custom Renderer? Thanks!

like image 475
cfly24 Avatar asked Mar 16 '17 21:03

cfly24


People also ask

What is Searchbar in Xamarin forms?

Xamarin.Forms SearchBar. The Xamarin.Forms SearchBar is a user input control used to initiating a search. The SearchBar control supports placeholder text, query input, search execution, and cancellation.

How do I instantiate a searchbar in XAML?

A SearchBar can be instantiated in XAML. Its optional Placeholder property can be set to define the hint text in the query input box. The default value for the Placeholder is an empty string so no placeholder will appear if it isn't set. The following example shows how to instantiate a SearchBar in XAML with the optional Placeholder property set:

How do I execute a search using the Searchbar control?

A search can be executed using the SearchBar control by attaching an event handler to one of the following events: SearchButtonPressed is called when the user either clicks the search button or presses the enter key. TextChanged is called anytime the text in the query box is changed.


2 Answers

You're right about creating a custom renderer, but in your renderer, to remove the underline, we need to find the plate of SearchView in native android first. You can for example code like this:

protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
    base.OnElementChanged(e);

    if (Control != null)
    {
        var plateId = Resources.GetIdentifier("android:id/search_plate", null, null);
        var plate = Control.FindViewById(plateId);
        plate.SetBackgroundColor(Android.Graphics.Color.Transparent);
    }
}
like image 126
Grace Feng Avatar answered Sep 19 '22 01:09

Grace Feng


This is to extend the answer above by @Grace.

In other examples I have came across following snippet:

protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
    base.OnElementChanged(e);

    if (Control != null)
    {
       LinearLayout linearLayout = this.Control.GetChildAt(0) as LinearLayout;
       linearLayout = linearLayout.GetChildAt(2) as LinearLayout;
       linearLayout = linearLayout.GetChildAt(1) as LinearLayout;

       linearLayout.Background = null; //removes underline
    }
}

However, on Android Lolipop and below linearLayout.Background = null; will result in

Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.setState(int[])' on a null object reference xamarin search bar

Hence I would replace the code above on my answer with answer from Grace's as follows:

if (Control != null)
{
    var plateId = Resources.GetIdentifier("android:id/search_plate", null, null);
    var plate = Control.FindViewById(plateId);
    plate.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
like image 28
envyM6 Avatar answered Sep 21 '22 01:09

envyM6