Harsh0021
Harsh0021

Reputation: 372

How to Recognise which menuItem is disabled?

Heyy,

I have implemented menu, where there are two menu items and when I click on first item, the other menu item goes disabled and vise-versa.

And when I click on back button navigation icon, I have to check if first item is disabled or not and if disabled, then turn the second item enabled, and if not then onBackPressed();

So, I don't know how to recognize which item is disabled.

Please help me fast.

There are some code references

This is my current try and it also have some errors. Please help me find another way or fix this code.

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (menu.getItem(0).getActionView().getVisibility() == View.VISIBLE) {
                    onBackPressed();
                } else {
                    menu.getItem(1).setEnabled(false);
                    menu.getItem(1).setVisible(false);

                    menu.getItem(0).setEnabled(true);
                    menu.getItem(0).setVisible(true);
                }
            }
        });

Upvotes: 2

Views: 172

Answers (1)

Zain
Zain

Reputation: 40830

in onCreateOptionsMenu() store the menu into a local class field, and then to check if a certain menu item is enabled/disabled use isEnabled()

MenuItem item = menu.findItem(R.id.item_id);
if (item.isEnabled()) {
    // enabled
} else {
    // not enabled 
}
 

Or you can use the order of the item among the menu items to get a particular item

MenuItem item = menu.getItem(0);

Upvotes: 2

Related Questions