Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 3.0 Honeycomb: How to enable/disable Menu Items in Action Bar?

it's pretty easy to disable a menu item in XML:

<item android:id="@+id/men_1" 
    android:title="@string/men_1" 
    android:showAsAction="ifRoom|withText"
    android:icon="@drawable/ic_menu_1"
    android:enabled="false"/>

It's also pretty easy to change it via code on a <3.0 app:

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
  super.onPrepareOptionsMenu(menu);

  MenuItem item = menu.findItem(R.id.men_1);
  item.setEnabled(false);

  return true;
}

But how would I do it on Android 3.x? I want to disable menu options depending on the Fragment shown.

Kind regards, jellyfish

like image 300
jellyfish Avatar asked Jun 21 '11 14:06

jellyfish


1 Answers

Pretty much the same but put code into the fragment instead, note different method signature.

@Override
public void onPrepareOptionsMenu(Menu menu) {
    MenuItem item= menu.findItem(R.id.men_1);
    item.setEnabled(false);
    super.onPrepareOptionsMenu(menu);
}

So the fragment takes responsibility for inflating the menu etc.

Edit Note the need to call setHasOptionsMenu(true)

like image 145
PJL Avatar answered Nov 17 '22 22:11

PJL