How to remove or change the search view icon inside the edittext? I am using the Appcompat library.
I used the below code to modify and remove but it's not working:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
View search_mag_icon = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
search_mag_icon.setBackgroundResource(R.drawable.ic_stub);
//search_mag_icon.setVisibility(View.GONE);
Finally found the solution. Try the following:
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drawable = (Drawable)mDrawable.get(your search view here);
drawable.setAlpha(0);
} catch (Exception e) {
e.printStackTrace();
}
Per the AppCompat v21 blog post, you can provide a custom style for your SearchView
, overriding things such as the search icon:
<style name="Theme.MyTheme" parent="Theme.AppCompat">
<item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
<!-- Search button icon -->
<item name="searchIcon">@drawable/custom_search_icon</item>
</style>
no a good idea use private method as SearchView.class.getDeclaredField("mSearchHintIcon");
<item android:id="@+id/menu_search"
android:icon="@drawable/action_search"
app:actionLayout="@layout/xx_layout"
app:showAsAction="always"/>
and in xx_layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:searchHintIcon="@null"
android:iconifiedByDefault="true" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With