I have implemented an android search view.When the search view gains focus, the close button [x] at the right shows up.How to hide close button in android search view
Asked
Active
Viewed 4,370 times
1
-
I think you can't do that, for that you must user custom sercview – N J Jul 07 '15 at 07:39
4 Answers
4
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.

Anton Kovalyov
- 932
- 4
- 13
1
I think, if you use support libraries, the first part of @Anton advice may not work, so it would be sufficient to write in values/styles.xml something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="searchViewStyle">@style/SearchViewStyleAfterV21</item>
</style>
...
<style name="SearchViewStyleAfterV21" parent="Widget.AppCompat.SearchView">
<item name="closeIcon">@android:color/transparent</item>
</style>

CoolMind
- 26,736
- 15
- 188
- 224
0
Have you tried this set 'x' button programicaly ?
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
In your XML layout file:
app:closeIcon="@android:color/transparent"
Then disable the button:
ImageView btnClose = searchView.findViewById(R.id.search_close_btn);
btnClose.setEnabled(false);

David Buck
- 3,752
- 35
- 31
- 35

Deddy Romnan Rumapea
- 184
- 3
- 8