Ks.P
Ks.P

Reputation: 222

Xamarin SearchBar remove cancel button on Android

How can I remove the cancel button in the search bar on Android?

enter image description here

Upvotes: 0

Views: 229

Answers (1)

Ax1le
Ax1le

Reputation: 6641

A simple approach could be:

<android.support.v7.widget.SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/searchView"
    app:closeIcon="@null"/>

If you need to disable its function, try to achieve it using code behind:

Android.Support.V7.Widget.SearchView searchView = FindViewById<Android.Support.V7.Widget.SearchView>(Resource.Id.searchView);
ImageView mCloseButton = (ImageView)searchView.FindViewById(Resource.Id.search_close_btn);
mCloseButton.Enabled = false;
mCloseButton.SetImageDrawable(null);

We need to use Android.Support.V7.Widget.SearchView here for retrieving the button or it will get null when using SearchView.

Update

I uploaded a simple sample here: https://github.com/landl0526/SearchViewSample.git

Here is the effect:

enter image description here

Upvotes: 1

Related Questions