Skip to content Skip to sidebar Skip to footer

How To Delete Or Change The Searchview Icon Inside The SearchView ActionBar?

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

Solution 1:

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();
}

Solution 2:

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>

Solution 3:

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" />

Solution 4:

You might use below lines of code:

int searchMagIcon = getResources().getIdentifier("search_mag_icon", "id", "android");
ImageView SearchHintIcon = (ImageView) searchView.findViewById(searchMagIcon);

That will save the ImageView and then you can change it.


Solution 5:

I used the menu item with the property app:actionViewClass="android.support.v7.widget.SearchView"

On the activity I set:

searchView = (SearchView) searchMenuItem.getActionView();
searchView.setIconifiedByDefault(false);

To remove the icon I did:

ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
searchIcon.setImageDrawable(null);

Post a Comment for "How To Delete Or Change The Searchview Icon Inside The SearchView ActionBar?"