How to change a menu item after it has already been created?

Asked

Viewed 119 times

4

The following I create a menu through the onCreateOptionsMenu method,.

@Override       
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.filter_menu, menu);
    mMenu=menu
    return true;
}

@Override

 public boolean onNavigationItemSelected(MenuItem item) {

    Menu id = navigationView.getMenu();

    if (id.getItem(0) == item) {
                     //essa é a parte que eu tento deixar invisível mas ela não fica
       mMenu.findItem(R.id.action_Filtro).setVisible(false);


    }       
}
  • The question is unclear. What are you testing with id.getItem(0) == item?

  • is the following then is a layout Drawer Fragment,so when I open it I wanted to take an item from the menu that does not fit for this Fragment. @ramaral

1 answer

2


It’s pretty simple if you can do something like this

boolean visible = false;

mMenu.findItem(R.id.action_Filtro).setVisible(visible);

when you need to change the visibility just change the boleano and call this method

invalidateOptionsMenu();

if you have fragmented only call by getactivity

Browser other questions tagged

You are not signed in. Login or sign up in order to post.