Android: changing drawable states of option menu items seems to have side-effects

Posted by pjv on Stack Overflow See other posts from Stack Overflow or by pjv
Published on 2011-01-02T13:42:07Z Indexed on 2011/01/02 13:53 UTC
Read the original article Hit count: 313

In my onCreateOptionsMenu() I have basically the following:

public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(Menu.NONE, MENU_ITEM_INSERT, Menu.NONE, R.string.item_menu_insert).setShortcut('3',
                'a').setIcon(android.R.drawable.ic_menu_add);

        PackageManager pm = getPackageManager();
        if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)){
            menu.add(Menu.NONE, MENU_ITEM_SCAN_ADD, Menu.NONE, ((Collectionista.DEBUG)?"DEBUG Scan and add item":getString(R.string.item_menu_scan_add))).setShortcut('4',
                    'a').setIcon(android.R.drawable.ic_menu_add);
        }
        ...
}

And in onPrepareOptionsMenu among others the following:

final boolean scanAvailable = ScanIntent.isInstalled(this);
        final MusicCDItemScanAddTask task = new MusicCDItemScanAddTask(this);          
        menu.findItem(MENU_ITEM_SCAN_ADD).setEnabled(scanAvailable && (tasks == null || !existsTask(task)));

As you see, two options items have the same drawable set (android.R.drawable.ic_menu_add). Now, if in onPrepareOptionsMenu the second menu item gets disabled, its label and icon become gray, but also the icon of the first menu item becomes gray, while the label of that fist menu item stays black and it remains clickable. What is causing this crosstalk between the two icons/drawables? Shouldn't the system handle things like mutate() in this case?

I've included a screenshot:

top menu item icon should not be gray

© Stack Overflow or respective owner

Related posts about android

Related posts about disable