MenuItem tinting on AppCompat Toolbar MenuItem tinting on AppCompat Toolbar android android

MenuItem tinting on AppCompat Toolbar


After the new Support library v22.1, you can use something similar to this:

  @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_home, menu);        Drawable drawable = menu.findItem(R.id.action_clear).getIcon();        drawable = DrawableCompat.wrap(drawable);        DrawableCompat.setTint(drawable, ContextCompat.getColor(this,R.color.textColorPrimary));        menu.findItem(R.id.action_clear).setIcon(drawable);        return true;    }


Setting a ColorFilter (tint) on a MenuItem is simple. Here is an example:

Drawable drawable = menuItem.getIcon();if (drawable != null) {    // If we don't mutate the drawable, then all drawable's with this id will have a color    // filter applied to it.    drawable.mutate();    drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);    drawable.setAlpha(alpha);}

The above code is very helpful if you want to support different themes and you don't want to have extra copies just for the color or transparency.

Click here for a helper class to set a ColorFilter on all the drawables in a menu, including the overflow icon.

In onCreateOptionsMenu(Menu menu) just call MenuColorizer.colorMenu(this, menu, color); after inflating your menu and voila; your icons are tinted.


app:iconTint attribute is implemented in SupportMenuInflater from the support library (at least in 28.0.0).

Tested successfully with API 15 and up.

Menu resource file:

<menu    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">    <item        android:id="@+id/menu_settings"        android:icon="@drawable/ic_settings_white_24dp"        app:iconTint="?attr/appIconColorEnabled"        <!-- using app name space instead of android -->        android:menuCategory="system"        android:orderInCategory="1"        android:title="@string/menu_settings"        app:showAsAction="never"        />    <item        android:id="@+id/menu_themes"        android:icon="@drawable/ic_palette_white_24dp"        app:iconTint="?attr/appIconColorEnabled"        android:menuCategory="system"        android:orderInCategory="2"        android:title="@string/menu_themes"        app:showAsAction="never"        />    <item        android:id="@+id/action_help"        android:icon="@drawable/ic_help_white_24dp"        app:iconTint="?attr/appIconColorEnabled"        android:menuCategory="system"        android:orderInCategory="3"        android:title="@string/menu_help"        app:showAsAction="never"        /></menu>

(In this case ?attr/appIconColorEnabled was a custom color attribute in the app's themes, and the icon resources were vector drawables.)