I found another way to change the search icon, which goes on the same line as Diego Pino's answer, but right in onPrepareOptionsMenu .
In your menu.xml (as before)
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_fav" android:title="@string/action_websearch" android:showAsAction="always|never" android:actionViewClass="android.widget.SearchView" /> </menu>
In your activity:
@Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem searchViewMenuItem = menu.findItem(R.id.action_search); mSearchView = (SearchView) searchViewMenuItem.getActionView(); int searchImgId = getResources().getIdentifier("android:id/search_button", null, null); ImageView v = (ImageView) mSearchView.findViewById(searchImgId); v.setImageResource(R.drawable.your_new_icon); mSearchView.setOnQueryTextListener(this); return super.onPrepareOptionsMenu(menu); }
I followed the example of changing the edit text in this example .
You should be able to do this for all the icons / backgrounds in your SearchView to find the correct id, which you can check here .
Hope this helps someone else!
UPDATE November 2017:
With this answer, the android has been updated with the ability to change the search icon via XML.
If you are aiming for anything below Android v21, you can use:
<android.support.v7.widget.SearchView android:layout_width="wrap_content" android:layout_height="match_parent" app:searchIcon="@drawable/ic_search_white_24dp" app:closeIcon="@drawable/ic_clear_white_24dp" />
Or v21 and later:
<SearchView android:layout_width="wrap_content" android:layout_height="match_parent" android:searchIcon="@drawable/ic_search_white_24dp" android:closeIcon="@drawable/ic_clear_white_24dp" />
And there are even more options:
closeIcon commitIcon goIcon searchHintIcon searchIcon voiceIcon
just_user Aug 21 '13 at 14:43 2013-08-21 14:43
source share