[FIXED] How to disable a particular option from optionmenu across all the tabs in android?

Issue

I am developing an application which consists of four tabs. Its also having six optionmenu which is common to all the four tabs and I need to disable one of the option menu depending on the current state of the application. Is there any way to disable particular option from the optionmenu across all the tabs??

Solution

To disable a menu item, you can override onPrepareOptionsMenu in your activity:

public boolean onPrepareOptionsMenu(Menu menu) {

    // To disable the item
    menu.findItem(R.id.the_menu_id).setEnabled(false);

    // To make the item invisible
    menu.findItem(R.id.the_menu_id).setVisibility(false);
}

If your tabs are composed of multiple views, then you only need to override the method in one place. If your tabs are composed of multiple activities, then you’ll need to override the method in each of your activities and choose which items to display based on your application’s state.

Answered By – Erich Douglass

Answer Checked By – Senaida (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published