Is it possible to grey out (not just disable) a MenuItem in Android? Is it possible to grey out (not just disable) a MenuItem in Android? android android

Is it possible to grey out (not just disable) a MenuItem in Android?


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:

@Overridepublic 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);    }}


I had the same issue. There are two ways of getting this to work:

  1. Put your icons in a StateList so that a different icon will be used on disable
  2. 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


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!