wbk727
wbk727

Reputation: 8408

Action bar menu item not tinting colour properly

I'm trying to tint a menu item colour to Color.WHITE but for some reason it's not working properly.

Before tinting

enter image description here

After tinting

enter image description here

    Drawable drawable = menu.findItem(R.id.action_info).getIcon();
    drawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
    menu.findItem(R.id.action_info).setIcon(drawable);

I don't understand why this has happened when all I want to do is change the icon colour from grey to white.

Upvotes: 1

Views: 1226

Answers (1)

Alex
Alex

Reputation: 1730

Might have to do with the DrawableCompat. I always do tinting using the ColorFilter with PorterDuff, as it allows to specify exactly what type of recolouring you want (SRC_IN mostly fits the result I want to achieve)

Try to change it like this:

Drawable drawable = menu.findItem(R.id.action_info).getIcon();
drawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
menu.findItem(R.id.action_info).setIcon(drawable); // Actualy, we don't need to do this

EDIT:

I see. This effect is caused by the image you are using. I guess you use the default icon ic_menu_info_details which has opacity built in. It is better to use your own icon in order to color it as needed.

Basically we can take the same icon, without opacity. And then this PorterDuff method works as expected (and probably your earlier code too).

You can easily find the icon and add it to your project.

Option 1:

Option 2:

  • Using vector drawables, then the images will be scaled automatically on each device too.

  • Right click on the drawable folder

  • Go to New -> Vector Asset

  • Click the change icon button, and find the Info icon

Dont forget to change your menu.xml so that you use the new icon

Upvotes: 1

Related Questions