Tint menu icons

guillaume-tgl picture guillaume-tgl · Jun 19, 2014 · Viewed 16.9k times · Source

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.

Thanks

Answer

jimejim picture jimejim · Jun 20, 2014

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(R.id.action_favorite);
Drawable newIcon = (Drawable)favoriteItem.getIcon();
newIcon.mutate().setColorFilter(Color.argb(255, 200, 200, 200), PorterDuff.Mode.SRC_IN);
favoriteItem.setIcon(newIcon);

You can also use a color resource like

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