Cheng
Cheng

Reputation: 551

How to tint sub-menu icon with a color selector?

I'm building an android app with several menu on actionbar. And here is the menu.xml.

<menu> 
 <item
        android:id="@+id/menu_comment"
        android:icon="@drawable/gsm_comment_selector"
        android:title="@string/COACH_COMMENT"
        app:showAsAction="always"/>

    <item
        android:id="@+id/menu_more"
        android:icon="@drawable/menu_more_selector"
        android:title="@string/MORE"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/menu_share"
                android:icon="@drawable/menu_share_selector"
                android:title="@string/SHARE"/>
            <item
                android:id="@+id/menu_help"
                android:icon="@drawable/menu_help_selector"
                android:title="@string/HELP"/>
        </menu>
    </item>
</menu>

And this is what a XXX_selector looks like. It contains a drawable and a template_selector(a color state list) so I can change the icon color when I click it.

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/ic_more_vert_white_24dp"
        android:tint="@color/template_selector"/>

The result is that when pressing menu_comment and menu_more, its icon color are changed. But menu_share and menu_help are not.

So what should I do to fix it? Thanks

Upvotes: 0

Views: 369

Answers (1)

John O&#39;Reilly
John O&#39;Reilly

Reputation: 10350

You should be able to do something like following (called from onCreateOptionsMenu)

    Drawable drawable = DrawableCompat.wrap(menuItem.getIcon());
    DrawableCompat.setTint(drawable.mutate(), context.getResources().getColor(tint));
    view.setIcon(drawable);

Upvotes: 1

Related Questions