Вопрос

How do I change the Android search view icon color? By default color is white, is it possible to change the icon color.

I have tried:

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

    <item
        android:id="@+id/action_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_theme_blue_menu"
        android:showAsAction="always"
        android:title="@string/search_actionbar"/>
    <item
        android:id="@+id/slider_menu"
        android:icon="@drawable/ic_slider_menu"
        android:orderInCategory="100"
        android:showAsAction="always"/>

</menu>

But I need to change it in code, not in the layout file. Is it possible?

Это было полезно?

Решение

Unfortunately, there is no easy way to change the SearchView icon to a custom drawable, since the theme attribute searchViewSearchIcon is not public. Check this answer for details.

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> 

Другие советы

What I do, I have create a method to change any drawable color

 public Drawable changeDrawableTint(Drawable drawable, int color){
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, ColorStateList
            .valueOf(color));
    return wrappedDrawable;
}

, even in android versions pre lollipop, then I get the search view menu item, and call the method with the desired color

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    /**
     * We inflate the correct activity menu
     */
    getMenuInflater().inflate(R.menu.menu_partner_list, menu);

    searchViewMenuItem = menu.findItem(R.id.action_search);
    if(searchViewMenuItem!=null && searchViewMenuItem.getIcon()!=null){
        searchViewMenuItem.setIcon(Utils.getInstance()
                .changeDrawableTint(searchViewMenuItem.getIcon(),
                        Color.WHITE));
    }
    setUpSearchView(menu);
    shouldHideMenuActions();
    return true;
}

here is the menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView"
        android:icon="@drawable/ic_search"
        android:title="@string/search" />

</menu>

you could use the following code in your java class:

mView.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.LIGHTEN);

This line above changes the color of the view to to a share of color you suggest in Color.parseColor() and a shade like how you define it using PorterDuff.Mode. Call the above code on click of a button to check if the color change is taking effect.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top