Question

This is the code I used to remove a MenuItem:

af.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

However, setShowsAsAction requires minimum API 11 so it crashes on GingerBread. I can use af.setVisible(false); to get the code to work on GingerBread. What are the differences between the two? Is it the same thing?

Was it helpful?

Solution

Use the v7 support library as followed:

MenuItem menuItem = menu.add(....);
MenuItemCompat.setShowAsAction(menuItem , MenuItemCompat.SHOW_AS_ACTION_NEVER);

OTHER TIPS

1. af.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

This will cause the MenuItem to be hidden fro the ActionBar. However it will be visible in the OverFlow Menu.

2. af.setVisible(false);

This will hide the MenuItem completely from the ActionBar and the Overflow Menu as well.

The difference might be when you set showAsAction to never, your menu item is still accessible with menu hardware or overflow button in ActionBar whereas when you use set visible method, your menu item is not displayed, even if you press the menu.

According to the reference, setVisible(boolean visible):

Sets the visibility of the MenuItem. If true the item will be visible; if false it is hidden (added in API 1)

And showAsAction method:

Sets how this item should display in the presence of an Action Bar (added in API 11)

As you can read, the mostly difference is showAsAction="never" is related to the ActionBar and hiding on it but still showing in overflow menu whereas setVisible since API 1 doesn't care about overflow menu and ActionBar: set to false, it litteraly hide your menu item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top