14

I am using appcompat library with a toolbar and I have implemented a search view in my app. But i want to remove the search view icon when its expanded.

here is my menu xml file:

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_search"
    android:title="@string/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"/>

and this is how I inflate the menu:

getMenuInflater().inflate(R.menu.main, menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setQueryHint(getString(R.string.search_hint));
return super.onCreateOptionsMenu(menu);

this is what i want to remove:

enter image description here

  • Check : http://stackoverflow.com/questions/14606294/remove-icon-logo-from-action-bar-on-android – Haresh Chhelana Dec 03 '14 at 06:42
  • Check [this](http://stackoverflow.com/questions/29633952/how-to-delete-or-change-the-searchview-icon-inside-the-searchview-actionbar/33229228#33229228) out – Sarasranglt Oct 20 '15 at 09:01
  • Have you considered that what you want the **is** a `EditText`? I had SO MANY problems trying to make simple things in the `SearchView` that i concluded that it wasn't designed for customization.. a simple edit text with some style solved my problem with the icon – Gabriel Rohden Oct 04 '18 at 17:35
  • Follow this question answer: https://stackoverflow.com/a/63988419/6160172 – Jamil Hasnine Tamim Sep 21 '20 at 08:11

11 Answers11

24

This helped me to remove Search icon

searchView.setIconified(false);

If you want searchView always expanded use just this:

searchView.onActionViewExpanded();
Rafael
  • 6,091
  • 5
  • 54
  • 79
17

Try this:

In your app theme, add the following line:

<item name="searchViewStyle">@style/MySearchViewStyle</item>

Add define the style at below:

<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
    <item name="searchHintIcon">@null</item>
</style>
hac.jack
  • 586
  • 5
  • 13
  • I prefer this solution over using private id's or reflection. Just make sure you are using the support version of the SearchView (android.support.v7.widget.SearchView) – jesusF10 Nov 14 '16 at 04:20
  • Does anyone know where to find the official documentation for the `searchViewStyle`? – PrashanD Aug 18 '18 at 09:09
5
try {
    Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
    mDrawable.setAccessible(true);
    Drawable drawable = (Drawable) mDrawable.get(searchView);
    drawable.setBounds(0, 0, 0, 0);
} catch (Exception e) {
    e.printStackTrace();
}

setBounds does the magic

4

The simplest way is Here... In case of AppCompat

app:iconifiedByDefault="false"
app:searchIcon="@null"

OR in other case

android:iconifiedByDefault="false"
android:searchIcon="@null"
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
4
<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
2

In your onCreateOptionsMenu

int searchPlateId = searchView.getContext().getResources()
            .getIdentifier("android:id/search_src_text", null, null);
    EditText searchPlate = (EditText) searchView
            .findViewById(searchPlateId);
    searchPlate.setHint("");
Dhinakaran Thennarasu
  • 3,336
  • 4
  • 26
  • 39
1

If you look at the android source code for the layout of SearchView , you will see that the search icon has a id search_mag_icon. So in onCreateOptionsMenu,

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search,menu);
    MenuItem searchViewItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView)searchViewItem.getActionView();        
    searchView.findViewById(R.id.search_mag_icon).setVisibility(View.GONE);
}

Just find the view by id and set its visibility to gone.

nupadhyaya
  • 1,909
  • 1
  • 14
  • 14
1

For androidx and new version of SearchView: If you add SearchView from code, it's effective for that. Add new style in styles.xm

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="searchHintIcon">@null</item>
</style>

and applied that style in your app theme:

<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
    <item name="searchViewStyle">@style/SearchViewMy</item>
</style>

Or if you add SearchView from xml, then add this line in your layout:

app:searchHintIcon="@null"
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
0

Look into SumeetP's answer in this question. Not sure if that works because I never used it. He gets Search icon as an ImageView. So I guess you can just set image to android.R.color.transparent so that it won't be visible.

Community
  • 1
  • 1
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

If you have a customized search view just add this in your .xml layout

app:searchIcon="@null" app:iconifiedByDefault="false"

Kenart
  • 285
  • 3
  • 14
0

That's an old question, but this helped me a lot in my case: If you are trying to hide inside a menu, none of the questions posted here worked perfectly for me (Greetings for AK5T that almost solved my problem). What solved the problem was: Instead of putting

app:actionViewClass="android.widget.SearchView"

inside the item in menu, you can change for:

app:actionLayout="@layout/search_layout"

And, inside this layout, you can add this piece of code:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.SearchView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:searchHintIcon="@drawable/nothing_drawed"/>

And, finally, inside the drawable you just create it with nothing inside, example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

</selector>

Only this method worked for me. When I tried to hide the searchHintIcon, setting to @null or anything else, it remained the same thing as if I had written no code.