Is it possible to grey out (not just disable) a MenuItem in Android?
Solution 1:
On all android versions, easiest way to use this to SHOW a menu action icon as disabled AND make it FUNCTION as disabled as well:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.menu_my_item);
if (myItemShouldBeEnabled) {
item.setEnabled(true);
item.getIcon().setAlpha(255);
} else {
// disabled
item.setEnabled(false);
item.getIcon().setAlpha(130);
}
}
Solution 2:
I had the same issue. There are two ways of getting this to work:
- Put your icons in a StateList so that a different icon will be used on disable
-
What I use now. Change the icon yourself with something like this in
onPrepareOptionsMenu()
:public boolean onPrepareOptionsMenu(Menu menu) { boolean menusEnabled = reachedEndOfSlidehow(); // enable or disable? MenuItem item = menu.findItem(R.id.menu_next_slide); Drawable resIcon = getResources().getDrawable(R.drawable.ic_next_slide); if (!menusEnabled) resIcon.mutate().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN); item.setEnabled(menusEnabled); // any text will be automatically disabled item.setIcon(resIcon); }
You can call invalidateOptionsMenu()
(or from ABS, supportInvalidateOptionsMenu()
) to rebuild the menu.
EDIT: Updated solution 2
Source: https://groups.google.com/forum/?fromgroups#!topic/actionbarsherlock/Z8Ic8djq-3o
Solution 3:
I found a new way to solve this issue using a drawable selector xml file. You just create a selector with the icon you want to use in your menu item, then you can either change the tint, alpha or both of the bitmap:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<bitmap android:src="@drawable/ic_menu_item"
android:tint="@color/enabled_color"
android:alpha="@integer/enabled_alpha"/>
</item>
<item android:state_enabled="false">
<bitmap android:src="@drawable/ic_menu_item"
android:tint="@color/disabled_color"
android:alpha="@integer/disabled_alpha"/>
</item>
</selector>
As a side note; I like to set the tint to "?android:attr/textColorPrimary"
for enabled state and "?android:attr/textColorHint"
for disabled state. This way it will adjust depending on the theme used.
Then you can just set the icon in your menu xml file to the selector resource:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_action"
android:orderInCategory="0"
android:title="@string/title_menu_action"
android:icon="@drawable/ic_menu_item_selector"
app:showAsAction="ifRoom"/>
</menu>
Then when you call item.setEnabled(enabled)
the color and/or alpha of the icon will change along with the state!
Solution 4:
The way I did it is by using "itemIconTint" in NavigationView, you can also grey out the text by using "itemTextColor"
This is Navigationview:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemBackground="@color/white"
android:background="@color/white"
app:itemTextColor="@color/menu_text_color"
app:itemIconTint="@color/menu_text_color"
app:menu="@menu/main_drawer" />
and the "@color/menu_text_color" is a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/primaryColor" />
<item android:state_enabled="false" android:color="@color/disabled_text_color" />
<item android:color="@color/primaryText" />
</selector>
Finally, if you want to disable a menuitem,
MenuItem item = mNavigationView.getMenu().findItem(R.id.your_menu_item);
item.setEnabled(isEnable);
Done!
Solution 5:
I had an issue where neither my the text nor the icon was visibly changing. The other answers either didn't work for me or weren't very elegant. Here's an answer that works for the latest Material recommendations.
You should be able to simply call menu.findItem(R.id.menu_my_item).isEnabled = false
in onPrepareOptionsMenu(menu: Menu)
.
(If you need onPrepareOptionsMenu
to run again, you can simply call invalidateOptionsMenu()
or activity?.invalidateOptionsMenu()
(from a fragment) and the application will queue up the menu to be recreated. Alternatively you can store off the menu item in a member variable to modify it later, but be careful to destroy your reference to it within onDestroyOptionsMenu
to avoid a memory leak.)
The fact that the menu item is disabled should be enough to grey out the text or the icon automatically. The difficulty is in setting up your styles to make this work.
Short Answer
First create a color state list my_color_state_list.xml
that you want your icons and text to use (e.g. black when enabled, grey when disabled). (See the full answer for an example.)
If you're using com.google.android.material.appbar.MaterialToolbar
, you can tell it to use this selector for icons and text by providing a custom theme overlay. In your activity's XML, give the toolbar the attribute android:theme="@style/Foo"
and define that style somewhere as:
<style name="Foo">
<item name="colorControlNormal">@color/my_color_state_list</item>
<item name="actionMenuTextColor">@color/my_color_state_list</item>
</style>
Now when the menu item is enabled or disabled via menu.findItem(R.id.menu_my_item).isEnabled = false
the text will automatically change color, and any icons which use the color ?attr/colorControlNormal
will also automatically change color.
Full answer
My starting place
My menu items are part of a Material toolbar. This answer may help for other kinds of toolbar/app bar, but your mileage may vary. In my activity I have something like this:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.MaterialComponents.Toolbar.Surface"/>
and the theme I'm using looks something like this:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/blue</item>
<item name="colorSecondary">@color/green</item>
<item name="colorSurface">@color/lightGrey</item>
<item name="colorOnSurface">@color/black</item>
[...]
<item name="windowActionModeOverlay">true</item>
</style>
It is also convention that the icon you use in buttons and menu items (and everywhere really) should have their default color be ?attr/colorControlNormal
. So for example I might have a vector image which looks like:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"
android:tintMode="src_atop">
<path android:pathData="..." android:fillColor="@android:color/white"/>
</vector>
If you download an icon from Material Icons you will see they all use colorControlNormal.
What I needed to do
If you look back at the definition of my toolbar, you will see it uses a ThemeOverlay ThemeOverlay.MaterialComponents.Toolbar.Surface
which is defined as:
<style name="ThemeOverlay.MaterialComponents.Toolbar.Surface" parent="">
<item name="colorControlNormal">@color/material_on_surface_emphasis_medium</item>
<item name="actionMenuTextColor">@color/material_on_surface_emphasis_medium</item>
</style>
This sets the menu item text color and icon color to @color/material_on_surface_emphasis_medium
which does not respond to being enabled or not. @color/material_on_surface_emphasis_medium
looks like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="@dimen/material_emphasis_medium" android:color="?attr/colorOnSurface"/>
</selector>
(You may be using ThemeOverlay.MaterialComponents.Toolbar.Primary
instead, which has a similar issue - it simply uses colorOnPrimary
.)
We need to replace this with our own color state list which responds to enabled state. So, make a new file res/color/menu_item_selector.xml
that looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:alpha="@dimen/material_emphasis_medium" android:color="?attr/colorOnSurface"/>
<item android:alpha="@dimen/material_emphasis_disabled" android:color="?attr/colorOnSurface"/>
</selector>
You see I've used the same conventions that the material library does by using their constants to define the alpha values, and I used colorOnSurface
as my color. If you were using ThemeOverlay.MaterialComponents.Toolbar.Primary
you would want colorOnPrimary
instead. Of course you can use any color or alpha here, it's up to you.
And now make a new ThemeOverlay in res/values/styles.xml
to point to this selector, inheriting from whatever ThemeOverlay you were using:
<!-- Toolbar - overrides the menu text color to use a selector that responds to whether it's enabled or not -->
<style name="ThemeOverlay.MyTheme.Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Surface">
<!-- Color used in the icons of menu actions (i.e. non-overflow menu items). This is just convention, this will affect anything that uses ?attr/colorControlNormal) -->
<item name="colorControlNormal">@color/menu_item_color_selector</item>
<!-- Color used in the text of menu actions (i.e. non-overflow menu items) -->
<item name="actionMenuTextColor">@color/menu_item_color_selector</item>
</style>
And now finally we can apply this ThemeOverlay
to the toolbar:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.MyTheme.Toolbar"/>