17

I've used "Android Action Bar Style Generator" in order to generate my own Holo theme. Then I've added two icons to the action bar, one for a searchFilter and one for a file picker, both icons are white. File picker icon (at the right) looks good, but somehow search icon looks dark grey. When typing on the search text view, close button also looks dark grey.

I've tried changing color of the actionbar and also from the searchview, without success. Why is the search view showing this color? How can I change it?

ActionBar with two actions

Thank you very much.

UPDATE: I thought that my app was using my icon but it doesn't, it uses the default search view icon, so my question actually is, how do I change the default icon of the search view? This sentence at the menu.xml file is not working:

android:icon="@drawable/selectable_header_search"
jmhostalet
  • 4,399
  • 4
  • 38
  • 47

6 Answers6

18

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.

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    ...
</style> 
Community
  • 1
  • 1
Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
10

Since the searchViewSearchIcon attribute is not public, you can change the icon using the following code:

int searchIconId = searchView.getContext().getResources().getIdentifier("android:id/search_button",null, null);
ImageView searchIcon = (ImageView) searchView.findViewById(searchIconId);
searchIcon.setImageResource(R.drawable........);
Amit Desale
  • 1,281
  • 3
  • 16
  • 43
mspapant
  • 1,860
  • 1
  • 22
  • 31
  • 3
    this throws a nullPointerException – desgraci Feb 19 '14 at 22:29
  • 4
    `ImageView searchIconView = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button); searchIconView.setImageResource(R.drawable.ic_nav_search);` will work on android 4+ – leegor Apr 16 '17 at 08:22
6

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);
timudgin
  • 61
  • 1
  • 1
  • 1
    This solved it (custom icon not working) for me, Android 4.1 to 5.0. Equivalently in menu XML: `app:showAsAction="always|collapseActionView"` – Jonik Apr 20 '15 at 16:38
0

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!

Community
  • 1
  • 1
just_user
  • 11,769
  • 19
  • 90
  • 135
0

In your theme style include this:

<style name="YourTheme.Toolbar">
    <item name="searchViewStyle">@style/YourActionBarSearch</item>
    <item name="editTextColor">@color/your_ab_text_color</item>
    <item name="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...".

<style name="YourActionBarSearch" parent="@style/Widget.Holo.SearchView">
    <item name="searchIcon">@drawable/ic_ab_search</item>
    <item name="queryBackground">@null</item>
    <item name="submitBackground">@null</item>
    <item name="searchHintIcon">@drawable/ic_ab_small_search</item>
    <item name="defaultQueryHint">@string/toolbar_search_hint</item>
    <item name="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
Per Löwgren
  • 767
  • 9
  • 17
-2

For those like me hoping to find a way to truly change the icon, you can use this with actionbar sherlock:

        // Getting the 'search_icon'
        ImageView searchIcon = (ImageView)searchView.findViewById(R.id.abs__search_button);
        // Setting background of 'search_plate' to earlier defined drawable.
        searchIcon.setImageResource(R.drawable.search_button_selector); 

You can't change the default one with the common way android:icon, because the field related to it had been set to private.

bugraoral
  • 2,630
  • 1
  • 21
  • 25