How to set icon color of MenuItem?
AndroidMenuitemAndroid Problem Overview
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
Android Solutions
Solution 1 - Android
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 - Android
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 - Android
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 - Android
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 - Android
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
Solution 6 - Android
color of icons in menu can change in Layout/activity_main.xml
set this line app:itemIconTint="@color/red_warning inside this tag com.google.android.material.navigation.NavigationView
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
app:itemIconTint="@color/red_warning"
/>
</androidx.drawerlayout.widget.DrawerLayout>
Solution 7 - Android
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_confirm, menu);
MenuItem action_done = menu.findItem(R.id.action_done);
action_done.setIcon(R.drawable.ic_filter);
Utils.menuIconColor(action_done, Color.WHITE);
super.onCreateOptionsMenu(menu, menuInflater);
}
public static void menuIconColor(MenuItem menuItem, int color) {
Drawable drawable = menuItem.getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
Solution 8 - Android
app:iconTint="@color/colorWhite"
add this in Navigational view properties
Ex -
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/main_color"
app:itemBackground="@drawable/divider_menu_items"
app:itemTextColor="@color/colorWhite"
app:itemIconTint="@color/colorWhite"
app:menu="@menu/activity_main_drawer"/>
Adding this all menu item icons convert to color given by you.
Solution 9 - Android
This behaviour is expected, as the ShareActionProvider
is
> responsible for creating views that enable data sharing and also to > show a sub menu with sharing activities if the hosting item is placed > on the overflow menu.
according to the documentation.
This means that you don't have control over the customization of the view when using it.