Set Visibility in Menu Programmatically Android

Set visibility in Menu programmatically android

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;

Android menu item visibility change in runtime

I retrieve the MenuItem instance as a field. [...] I was wondering if this is a good approach or should I somehow use the invalidateOptionsMenu() to make it visible again?

This is not the recommended way to do what you're trying. You're better off using supportInvalidateOptionsMenu() to trigger onPrepareOptionsMenu() and a boolean field or method to check to then set the visibility of the item directly.

Can I ran into null pointer exceptions?

In recent versions of Android, it is possible for onPrepareOptionsMenu() to be called before the Menu has been inflated via onCreateOptionsMenu(). So you should always perform a null check on the results of menu.findItem(), or otherwise protect against this scenario (perhaps by checking if menu.getSize() > 0).

How to set visibility for an actionbar menu group?

Finally found the solution
I needed to use the setGroupVisible() method of the menu object passed into the onPrepareOptionsMenu() method

This is what worked for me

Instead of

MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuImage.setEnabled(mEnableImageMenu);
mnuTextGroup.setVisible(false);

This is what I needed

menu.setGroupVisible(R.id.mnu_text_group, false);

How do I hide a menu item in the actionbar?

Get a MenuItem pointing to such item, call setVisible on it to adjust its visibility and then call invalidateOptionsMenu() on your activity so the ActionBar menu is adjusted accordingly.

Update: A MenuItem is not a regular view that's part of your layout. Its something special, completely different. Your code returns null for item and that's causing the crash. What you need instead is to do:

MenuItem item = menu.findItem(R.id.addAction);

Here is the sequence in which you should call:
first call invalidateOptionsMenu() and then inside onCreateOptionsMenu(Menu) obtain a reference to the MenuItem (by calling menu.findItem()) and call setVisible() on it

Set Visibility Of Android Menu Item Outside of onPrepareOptionsMenu

I see two options:

Use onPrepareOptionsMenu() anyway

This is what I'd recommend. In whatever async callback you're using, modify the value of a Boolean flag or other similar logic, and then call invalidateOptionsMenu(), which will trigger onPrepareOptionsMenu() whenever you want:

private var showItem = false

fun example() {
doAsync { resultCode ->
showItem = (resultCode == 200)
invalidateOptionsMenu()
}
}

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
val menuitem = menu?.findItem(R.id.action_edit_product)
if (menuitem != null) {
menuitem.setVisible(showItem) // reference the flag here
}
return true
}

Store the MenuItems when you create them

It is valid to save references to MenuItem objects generated in onCreateOptionsMenu(). You could save the item off at that time and then update it directly from your async callback:

private lateinit var menuitem: MenuItem

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.toolbar_menu, menu)
this.menuitem = menu.findItem(R.id.action_edit_product)
return true
}

fun example() {
doAsync { resultCode ->
menuitem.setVisible(resultCode == 200)
}
}

How to change visibility for MenuItem dynamically?

You need to set the menu visible in the onPrepareOptionsMenu(). You could change your code as follow:

private boolean menuShow = false;

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition()==1) {
menuShow = true;
}
}

public boolean onPrepareOptionsMenu(Menu menu) {
if(menuShow){
mOptionsMenu.getItem(0).setVisible(true);
mOptionsMenu.getItem(1).setVisible(true);
}
return true;
}

How to set visibility for an Actionbar menu item?

Try this following code:
call this function whereever you want hideItem()

  private void hideItem()
{

Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_adduser).setVisible(false);
}

How to dynamically hide a menu item in BottomNavigationView?

mBottomNavigationView.getMenu().removeItem(R.id.item_name);

removeItem does the trick. Not sure why setVisible method is not working.



Related Topics



Leave a reply



Submit