android - Menu item stays white even disabled

Viewed 451

Graying out the Save item in toolbar only happens in design mode but never on runtime.

enter image description here

I'm using this code:

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(count > 0) {
        menuItemSave.setEnabled(true);

    } else {
        menuItemSave.setEnabled(false);
    }

    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    menuItemSave = menu.findItem(R.id.save);

    return true;
}

It's enabling/disabling but it just really not turning into gray when disabled.

2 Answers

You can use custom color to change color when button is enabled or disabled. [ Change color according to your requirement ] colors/custom_button_color.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#FF0000" android:state_enabled="false"  />
    <item android:color="#CCCCCC"/>
</selector>

In menu.xml file set to your required button item like

android:textColor="@color/custom_button_color"

According to Android docs onPrepareOptionsMenu is called just before menu is opened.But if menu is always open, it will not be called.

So,

On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu that is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items.

Related