1

I am trying to customize the searchview. i wanted to get rid of close icon in searchview. what i have done so far?

int closeId =android.support.v7.appcompat.R.id.search_close_btn;
ImageView close = (ImageView)mSearchView.findViewById(closeId);
close.setVisibility(View.GONE);

this code removes the close icon. But as soon as i start typing in editText it appears back in. How to permanently get rid of it?

close icon is remove when expanded searchView

enter image description here

it comes back in when i type in editText. enter image description here

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74

3 Answers3

0

Tried this?: Hide close button in android Search View

Change your theme like this:

For < v21:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarWidgetTheme">@style/AppTheme.WidgetTheme</item>
</style>

<style name="AppTheme.WidgetTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="searchViewCloseIcon">@android:color/transparent</item>
</style>

For v21 and above:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>

<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
    <item name="closeIcon">@android:color/transparent</item>
</style>

Also check this for more customization.

Community
  • 1
  • 1
DenseCrab
  • 1,273
  • 11
  • 22
0

In case you want do it programicaly, put a piece of code that hides "x" button in onQueryTextListener:

   searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String query) {
            ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
            searchViewIcon.setVisibility(View.GONE);
            return true;
        }
   });
wapn
  • 359
  • 1
  • 7
  • 19
0

Add this attribute to your SearchView xml layout file :

app:closeIcon="@android:color/transparent"

Then disable the close button :

ImageView btnClose = searchView.findViewById(R.id.search_close_btn);
btnClose.setEnabled(false);