android – Tint menu icons-ThrowExceptions

Exception or error:

I’m building an Android app and I use the icons from the Action Bar Icon Pack to use in the action bar. I define them through the xml files in the menu folder.
Is there a way to “tint” these icons so that they are all the same color?
So far, I have to do it manually with an image editing software but if I decide to change the color, I have to do it all over again.

I know there is a android:tint attribute for ImageView but I haven’t found a way to use it for the menu’s icons.


How to solve:

There may be a better way to do this, but one option is to redraw the icon in code.

Suppose you have a menu item for favorites and want to tint it gray:

MenuItem favoriteItem = menu.findItem(;
Drawable newIcon = (Drawable)favoriteItem.getIcon();
newIcon.mutate().setColorFilter(Color.argb(255, 200, 200, 200), PorterDuff.Mode.SRC_IN);

You can also use a color resource like

newIcon.mutate().setColorFilter(getResources().getColor(R.color.myCustomTint), PorterDuff.Mode.SRC_IN);


If original source of icon is raster image then it’s possible to wrap it by <bitmap>.

Add this file into drawable folder – settings_icon.xml:

<bitmap xmlns:android=""

and then use this drawable for menu item:

<menu xmlns:android=""
<item android:id="@+id/new_game"


Now you can use tinting from DrawableCompat rather than color filter:

MenuItem favoriteItem = menu.findItem(;
Drawable favoriteIcon = DrawableCompat.wrap(favoriteItem.getIcon());
ColorStateList colorSelector = ResourcesCompat.getColorStateList(getResources(), R.color.tinted_selector, getTheme());
DrawableCompat.setTintList(favoriteIcon, colorSelector);


To improve you can create a static method in a utils class and use that method everytimes you want.

——– DEFINE YOUR METHOD———————————–

public static void tintMenuIcon(Context context, MenuItem item, @ColorRes int color) {  
    Drawable normalDrawable = item.getIcon();
    Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
    DrawableCompat.setTint(wrapDrawable, context.getResources().getColor(color));


——————— HOW USE IT ——————————-

public boolean onCreateOptionsMenu(Menu menu) {  
    getMenuInflater().inflate(, menu);
    MenuItem menuItem = menu.findItem(;

    if (menuItem != null) {
        tintMenuIcon(MainActivity.this, menuItem, android.R.color.holo_purple);//HERE

    return true;

My source was :

Leave a Reply

Your email address will not be published. Required fields are marked *