Skip to content Skip to sidebar Skip to footer

Removing Default Search Icon From Searchview Widget

How can i remove default search icon which appears as a hint in SearchView widget(not in actionbar)?Through styles i can change the icon but i can't find a way to remove it?

Solution 1:

It's Worked......

ImageViewsearchViewIcon= (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

ViewGrouplinearLayoutSearchView=(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);

Solution 2:

For me it only worked this:

ImageViewmagImage= (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
magImage.setImageDrawable(null);

@sector11's almost got it, but still you need to add the line magImage.setImageDrawable(null) because taking a look at SearchView.java in Github,specifically the method updateViewsVisibility, you can see that it constantly gets its visibility updated

Extract from GitHub

if (mCollapsedIcon.getDrawable() == null || mIconifiedByDefault) {
    iconVisibility = GONE;
} else {
    iconVisibility = VISIBLE;
}
mCollapsedIcon.setVisibility(iconVisibility);

Solution 3:

As of now you should use

ImageViewmagImage= (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);

Solution 4:

Try this one:

<stylename="SearchViewStyle"parent="Widget.AppCompat.SearchView" ><itemname="searchHintIcon">@null</item><!-- Background for the search query section (e.g. EditText) --><itemname="queryBackground">@color/transparent</item></style>

Never use direct android ids to find the view because in future if the ids change, your code will not work. Always use standard ways to solve the problems instead of a workaround. for more info refer this link.

Solution 5:

For androidx SearchView just use app:searchIcon="@null"

Post a Comment for "Removing Default Search Icon From Searchview Widget"