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?

- 1
- 1

- 9,157
- 18
- 82
- 139
7 Answers
It's Worked......
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);

- 3,131
- 5
- 26
- 51

- 213
- 2
- 6
-
Best answer of the lot – Smit Davda May 31 '17 at 10:10
-
1For androidx users use this `ImageView searchViewIcon = (ImageView)searchView.findViewById(androidx.appcompat.R.id.search_mag_icon); searchViewIcon.setVisibility(View.GONE);` – digiwizkid Nov 20 '19 at 07:39
For me it only worked this:
ImageView magImage = (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);

- 1
- 1

- 5,818
- 5
- 42
- 58
For androidx SearchView just use app:searchIcon="@null"

- 3,334
- 1
- 22
- 27
-
2only works for the plain view, once the search bar is clicked the icons still there – Erdnuss Oct 04 '19 at 14:14
As of now you should use
ImageView magImage = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);

- 6,730
- 6
- 39
- 81
Try this one:
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView" >
<item name="searchHintIcon">@null</item>
<!-- Background for the search query section (e.g. EditText) -->
<item name="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.

- 3,418
- 1
- 19
- 34

- 186
- 1
- 5
you can override it:
int searchImgId = context.getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView searchImage = (ImageView) searchView.findViewById(searchImgId);
searchImage.setVisibility(View.GONE);
If your search view is in layout then please use EditText with close button:
If you want to create your own then use frame layout and add text change listener to mange visibility of close or cancel button.
there are few discussion regarding search view in UI:
How to create EditText with cross(x) button at end of it?
How to place button inside of edit text
If you want to show search list then use AutoCompleteTextView instead of EditText.
you can read tutorial from below link:
I hope it will work.
-
1it gives NullPointerException on `searchImage.setLayoutParams`..i placed this inside onCreate() of activity – Android Developer Apr 09 '15 at 06:21
-
-
getting nullpointerexception on `searchImage.setVisibility(View.GONE);` – Android Developer Apr 09 '15 at 06:38
-
-
my searchview widget is iconified..when i click on icon it expands ..i am NOT using `setIconifiedByDefault(false)` – Android Developer Apr 09 '15 at 06:41
-
-
Yeah I want to remove search icon from expandable search view – Android Developer Apr 09 '15 at 07:04
-
Instead of `context.getResources()` i tried `getApplicationContext().getResources()` ,`getBaseContext().getResources()` and just `getResources()` but none of them worked..What should be there in `context`? – Android Developer Apr 09 '15 at 08:12
-
My SearchView is NOT a part of action bar..maybe I need to use something different there – Android Developer Apr 09 '15 at 15:01
-
-
but i need to display suggestion list also.i have used editext with cross button previously where suggestion list is not required..but here suggestion list is required – Android Developer Apr 10 '15 at 05:53
-
please set this SearchView in your code
android:paddingLeft="-16dp" // this line is remove the space of icon
android:paddingStart="-16dp" // // this line is remove the space of icon
android:searchIcon="@null" // this line to remove the search icon
android:closeIcon="@null" // this line to remove the close icon
<SearchView
android:queryBackground="@android:color/transparent"
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="-16dp"
android:paddingStart="-16dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@null"
android:searchIcon="@null"
android:closeIcon="@null"
android:theme="@style/BaseTheme"
android:iconifiedByDefault="false"
android:queryHint="@string/hint">
<requestFocus />
</SearchView>

- 1,741
- 19
- 35

- 21
- 6