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

4

This is what I create a menu through the onCreateOptionsMenu method, and then I want to leave an item invisible but I can not.

@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);


    }       
}
    
asked by anonymous 29.12.2016 / 22:14

1 answer

2

Opá is very simple if you can do something like this

boolean visible = false;

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

When you need to change visibility, just change boolean and call this method

invalidateOptionsMenu();

If you are in fragment, just call by getactivity

    
31.12.2016 / 05:11