How to set icon color of MenuItem?

I defined a menu item that has ShareActionProvider and share white icon like so :

<item
    android:icon="@drawable/ic_share_white_24dp"
    android:id="@+id/action_share"
    android:title="@string/action_share"
    android:orderInCategory="200"
    app:showAsAction="ifRoom"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

But when I launch the application, I get a different black share icon. How to set the share icon to be white?

Here is the result that I have

enter image description here


Solution 1:

The icon is actually provided by the ShareActionProvider and you can't change it afaik. You can, however, customize the color by setting the textColorPrimary in your styles.xml:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/MyActionBarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<style name="MyActionBarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">#fa0</item>
</style>

For any custom icons, you would have to color them yourself, ie.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    for(int i = 0; i < menu.size(); i++){
        Drawable drawable = menu.getItem(i).getIcon();
        if(drawable != null) {
            drawable.mutate();
            drawable.setColorFilter(getResources().getColor(R.color.textColorPrimary), PorterDuff.Mode.SRC_ATOP);
        }
    }

    return true;
}

Solution 2:

Short and Sweet Answer--> app:iconTint="@color/yourcolor

add app:iconTint="@color/yourcolor" in your MenuItem for change the Icon color.

<item
    android:icon="@drawable/ic_share_white_24dp"
    android:id="@+id/action_share"
    android:title="@string/action_share"
    android:orderInCategory="200"
    app:iconTint="@color/yourcolor"
    app:showAsAction="ifRoom"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

Solution 3:

This is a theming issue. Depending on your current theme, you need to set the correct ActionBar overlay theme. The Action Provider reads a value in the theme (which indicates if the theme is dark or light) to determine the color of the icon.

If your main theme is light and your ActionBar is dark, your ActionBar/Toolbar must use the theme ThemeOverlay.AppCompat.Dark.ActionBar.

Solution 4:

try this :

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.MENU, menu);

    // change color for icon 0 
    Drawable yourdrawable = menu.getItem(0).getIcon(); // change 0 with 1,2 ... 
    yourdrawable.mutate();
    yourdrawable.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_IN);
    return true;
}       

Solution 5:

short Answer --> use app:iconTint="?android:textColorPrimary" if you want the icon color to be white, write: android:theme = "@style/ThemeOverlay.MaterialComponents.Dark.ActionBar" else if you want black color, write: android:theme="@style/ThemeOverlay.MaterialComponents.Light" to your toolbar