6

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);
tux3
  • 7,171
  • 6
  • 39
  • 51
Siri
  • 701
  • 1
  • 6
  • 27

9 Answers9

24

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();
}
Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
Sarasranglt
  • 3,819
  • 4
  • 23
  • 36
  • 1
    After trying everything, it was the only solution working for me, thanks – Chol Apr 20 '16 at 14:13
  • 2
    adding this line **drawable.setBounds(0,0,0,0)** enables you to display hints with 0 left padding otherwise hints are displayed after icon which is still exists transparently. – SpiralDev Dec 15 '16 at 17:29
  • 1
    It works but shows a warning **detects reflective access to fields `mSearchHintIcon` and methods which don't exist or aren't visible** – Aashish Kumar Mar 30 '18 at 18:51
4

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>
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
2

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" />
AK5T
  • 114
  • 3
1

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.

dhun
  • 643
  • 7
  • 13
1

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);
Roni Castro
  • 1,968
  • 21
  • 40
0

remove is the right drawable that you have mentioned in xml:

final Drawable remove = ContextCompat.getDrawable(getActivity(), R.drawable.ic_close_circle);
        etSearchProducts.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (etSearchProducts.getCompoundDrawables()[2] == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (event.getX() > etSearchProducts.getWidth() - etSearchProducts.getPaddingRight() - remove.getIntrinsicWidth()) {
                    etSearchProducts.setText("");
                    Utility.hideKeyboard(getActivity());
                }

                return false;
            }
        });
Ravi Yadav
  • 2,296
  • 3
  • 25
  • 32
0

Simplest way is

  1. In case you are using AppCompat use

        app:iconifiedByDefault="false"
        app:searchIcon="@null"
    
  2. Else use android in place of app in above code as

    android:iconifiedByDefault="false"
    android:searchIcon="@null"
    
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
0

I was digging about that a lot and finally I found this solution and it works for me!
You can use this

If you use appcompat try this:

ImageView searchIconView = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIconView.setImageResource(R.drawable.yourIcon);

If you use androidx try this:

ImageView searchIconView = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_button);
    searchIconView.setImageResource(R.drawable.yourIcon);

It will change the default serchview icon.

Hope it helps!

  • Just to let you know that posting multiple identical answers to different questions will get automatically flagged for moderators to review. If you do come across questions to which a single answer would be able to answer both questions, you should consider flagging one of them as a duplicate of the other. – David Buck Jun 28 '20 at 12:31
0

Simply set the searchHintIcon property as null in your Searchview.

app:searchHintIcon="@null"

Aishwarya
  • 151
  • 1
  • 5