Kyryl Zotov
Kyryl Zotov

Reputation: 1968

Android set menu action visibility

I have Activity and a Fragment with a android.support.v7.widget.Toolbar

Can I show menu item "action" after creating menu in onCreateOptionsMenu() by making it visible or something ?

(After clicking custom button for example)

Upvotes: 0

Views: 316

Answers (2)

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

Can I show menu item "action" after creating menu in onCreateOptionsMenu() by making it visible or something ?

yes by overriding onPrepareOptionsMenu method : Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents. you make action item visible or gone like below(just sample code).

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
     MenuItem menuItemVadd = menu.findItem(R.id.action_vadd);
     if (isShowVisualAdd1) {
            menuItemVadd.setVisible(true);
     } else {
            menuItemVadd.setVisible(false);
     }
     return super.onPrepareOptionsMenu(menu);
}

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317372

You can use onPrepareOptionsMenu to modify the menu just before it's displayed when the user clicks the menu button. As stated by the javadoc:

This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

Upvotes: 1

Related Questions