How To Change Searchview Default Icon?
Solution 1:
Unfortunately, there is no easy way to change the SearchView
icon to a custom drawable, since the theme attribute searchViewSearchIcon
is not public. See this answer for details.
However, I think that your problem is caused by inheriting from the wrong theme. Please use android:Theme.Holo.Light.DarkActionBar
as the base for your theme. Then the default icons on the action bar should have a light color.
<stylename="AppTheme"parent="android:Theme.Holo.Light.DarkActionBar">
...
</style>
Solution 2:
Since the searchViewSearchIcon attribute is not public, you can change the icon using the following code:
intsearchIconId= searchView.getContext().getResources().getIdentifier("android:id/search_button",null, null);
ImageViewsearchIcon= (ImageView) searchView.findViewById(searchIconId);
searchIcon.setImageResource(R.drawable........);
Solution 3:
Try to set up in the showAsAction this option: *MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW*
Without this option the SearchView do not take the icon which you setup for action button.
menu.add("Search")
.setIcon(
getResources().getDrawable(
R.drawable.selectable_header_search))
.setActionView(searchView)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
Solution 4:
The easiest way I've found to change the default searchview icon in the action bar is in your onPrepareOptionsMenu. You can see my answer here to a similar question!
Solution 5:
In your theme style include this:
<stylename="YourTheme.Toolbar"><itemname="searchViewStyle">@style/YourActionBarSearch</item><itemname="editTextColor">@color/your_ab_text_color</item><itemname="android:textColorHint">@color/your_ab_hint_color</item></style>
You can now define the style of the SearchView with searchViewStyle
, and editTextColor
will set the color of the text in the search box. By setting android:textColorHint
you will also set the color of the hint, e.g. "Search...".
<stylename="YourActionBarSearch"parent="@style/Widget.Holo.SearchView"><itemname="searchIcon">@drawable/ic_ab_search</item><itemname="queryBackground">@null</item><itemname="submitBackground">@null</item><itemname="searchHintIcon">@drawable/ic_ab_small_search</item><itemname="defaultQueryHint">@string/toolbar_search_hint</item><itemname="closeIcon">@drawable/ic_ab_small_search_close</item></style>
This will style your search view as you like, more or less. The field searchIcon
is the icon in the action bar. The field searchHintIcon
is the small icon to the left of the hint in the search field. The field closeIcon
is the close icon at the right side of the search field.
Check this out, for more style settings:
$ANDROID_SDK/platforms/android-24/data/res/values/styles_holo.xml
Post a Comment for "How To Change Searchview Default Icon?"