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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With