Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete or change the searchview icon inside the SearchView actionBar?

screenshot

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);
like image 759
Siri Avatar asked Apr 14 '15 17:04

Siri


3 Answers

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();
}
like image 139
Sarasranglt Avatar answered Nov 02 '22 23:11

Sarasranglt


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>
like image 30
ianhanniballake Avatar answered Nov 03 '22 00:11

ianhanniballake


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" />
like image 45
AK5T Avatar answered Nov 02 '22 23:11

AK5T