Siri
Siri

Reputation: 701

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

Upvotes: 6

Views: 8520

Answers (9)

Aishwarya
Aishwarya

Reputation: 361

Simply set the searchHintIcon property as null in your Searchview.

app:searchHintIcon="@null"

Upvotes: 0

Marawan Walid
Marawan Walid

Reputation: 13

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!

Upvotes: 0

AK5T
AK5T

Reputation: 124

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

Upvotes: 2

Inzimam Tariq IT
Inzimam Tariq IT

Reputation: 6748

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"
    

Upvotes: 0

Roni Castro
Roni Castro

Reputation: 2134

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

Upvotes: 1

Ravi Yadav
Ravi Yadav

Reputation: 2386

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

Upvotes: 0

Sarasranglt
Sarasranglt

Reputation: 3879

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

Upvotes: 24

ianhanniballake
ianhanniballake

Reputation: 199805

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>

Upvotes: 4

dhun
dhun

Reputation: 643

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.

Upvotes: 1

Related Questions