ABS menu icons: setEnabled(false)

4,293 views
Skip to first unread message

Oleg Vaskevich

unread,
Jun 4, 2012, 2:48:03 AM6/4/12
to actionba...@googlegroups.com
In my app, I need to disable menu items depending on the state of a database. I do this with SherlockActivity.supportInvalidateOptionsMenu() and calling setEnabled(false) on the menu items I want to disable. While this does disable the text, the icons remain the same - they are not "grayed out" or anything.

Is this intended behavior? If not, I'd still like to give the icons a disabled look. Looks like there are two options:

  1. Use a StateListDrawable to set disabled versions of icon files
  2. Look to see how Android makes icons disabled on previous versions. This could be possibly be done using setAlpha() or setColorFilter(...)
Any suggestions (esp. regarding where to look for #2 - MenuItem.java? That's just an interface) would be appreciated.

Jake Wharton

unread,
Jun 4, 2012, 3:24:28 AM6/4/12
to actionba...@googlegroups.com
The only possibility is option 1. You cannot accomplish option 2 with the existing APIs. You can, however, change the icon yourself if the item is disabled.

---
Jake Wharton
http://about.me/jakewharton

Sérgio Faria

unread,
Jun 4, 2012, 11:51:02 AM6/4/12
to actionba...@googlegroups.com
I use a color filter, I've no idea how close the effect is to the "native filter", but looks great.

    /**
     * Mutates and applies a filter that converts the given drawable to a Gray image. This method
     * may be used to simulate the color of disable icons in Honeycomb's ActionBar.
     *
     * @return a mutated version of the given drawable with a color filter applied.
     */
    public static Drawable convertDrawableToGrayScale(Drawable drawable) {
        if (drawable == null) {
            return null;
        }

        Drawable res = drawable.mutate();
        res.setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
        return res;

Oleg Vaskevich

unread,
Jun 4, 2012, 12:08:29 PM6/4/12
to actionba...@googlegroups.com
That works great - thank you!

Nobu Games

unread,
Jun 19, 2012, 8:15:46 PM6/19/12
to actionba...@googlegroups.com
The Android design guidelines for iconography say that disabled action bar icons should have an opacity of 30% (as opposed to enabled state with opacity 60% for the dark theme or 80% for the light theme). The guidelines also state that action bar icons should be either a specific shade of gray or white (and not colored).

For coping with that issue I'm using following workaround:

menuItem.getIcon().setAlpha(enabled ? 255 : 64);

255 in case of enabled because my icon graphics already have the required opacity of 60%.
64 for the disabled icon state because it looked best in my case (using a light theme).

Actually I was expecting that there is some default behavior that takes care of that automatically. But I'm getting the impression that Google simply lets some room for individual theme customizations that have nothing much in common with the guidelines - like for example colored action bar items and different disabled state looks.
Reply all
Reply to author
Forward
0 new messages