Set visibility in Menu programmatically android
Asked Answered
S

6

62

So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

public boolean onOptionsItemSelected (MenuItem item){
    switch (item.getItemId()){
        case R.id.menuregistrar:
            break;
        case R.id.menusalir:
            break;
    }
    return true;
}

But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.

Soteriology answered 27/1, 2012 at 7:39 Comment(0)
P
160

Put this method in your Activity

public boolean onPrepareOptionsMenu(Menu menu)
{
    MenuItem register = menu.findItem(R.id.menuregistrar);      
    if(userRegistered) 
    {           
        register.setVisible(false);
    }
    else
    {
        register.setVisible(true);
    }
    return true;
}

in shorter version you could write:

MenuItem register = menu.findItem(R.id.menuregistrar);      
register.setVisible(!userRegistered);  //userRegistered is boolean, pointing if the user has registered or not.
return true;
Pascoe answered 27/1, 2012 at 7:44 Comment(4)
Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?Dartmoor
You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.Succursal
Is there a way to set the menuitem GONE? Because it is taking space while not beeing seen...Giulietta
It doesn't work for me. It can't find the menu item.Renelle
S
10

I would simplify Adil's solution even further with the following:

public boolean onPrepareOptionsMenu(Menu menu)
{
    MenuItem registrar = menu.findItem(R.id.menuregistrar);      
    registrar.setVisible(!isRegistered);
    return true;
}
Softener answered 12/12, 2012 at 19:40 Comment(2)
Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)Pascoe
@AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.Gooey
U
6

Simply do one thing get the id of the item of menu from this line:

Menu menu =navigationView.getMenu();

MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);

and than make it visible it accourding to you by this line:

nav_dashboard.setVisible(true/false);
Unruly answered 21/11, 2018 at 12:40 Comment(0)
S
5

Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//

Example

private Menu menu_change_language;
...

...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    ...
    ...
    menu_change_language = menu;
    ...
    ...

    return super.onCreateOptionsMenu(menu);
}

use code below for hiding Menu Item:

if(menu_change_language != null){                 
    menu_change_language.findItem(R.id.menu_change_language)
       .setVisible(false);
}
Surpassing answered 26/3, 2018 at 8:15 Comment(2)
this is useful if you want to visible menu after some certain operation. Thanks.Merodach
Kindly vote or promote this answer if you find it useful. ThanksSurpassing
S
4

Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.

Cheers

Selfappointed answered 27/1, 2012 at 7:45 Comment(2)
onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.Underclothing
@Underclothing this call will only work if Build.VERSION.SDK_INT >= 11Selfappointed
D
4

If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   item.setVisible(true);
   return true;
}

OR for item in the menu that you didn't click on

private Menu globalMenuItem;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu."menu Xml Name", menu);
   globalMenuItem = menu;
   return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
   globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
   return true;
}
Dogwood answered 18/11, 2018 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.