Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically hide a menu item in BottomNavigationView?

I want to hide a menu item of BottomNavigationView dynamically based on some conditions. I tried the following but it is not working.

mBottomNavigationView.getMenu()
            .findItem(R.id.item_name)
            .setVisible(false);

mBottomNavigationView.invalidate();
like image 398
Ram Avatar asked Jul 12 '17 07:07

Ram


People also ask

How do I hide a menu item?

If you want to change the visibility of your menu items on the go you just need to set a member variable in your activity to remember that you want to hide the menu and call invalidateOptionsMenu() and hide the items in your overridden onCreateOptionsMenu(...) method.


3 Answers

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

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

like image 196
Ram Avatar answered Oct 24 '22 03:10

Ram


You can hide a menu item by setting isVisible as false with using suggested property isVisible in Kotlin. But this makes your menu item removed from BottomNavigationView on Android 9 as my observation.

bottomNavigation.menu.findItem(R.id.menu_item).isVisible = false

If you use a single color for your bottom navigation view's background you can use similar approach to save the menu items in place. As an example the one in the right edge.

// 0x000000 is black as an example
bottomNavigation.menu.findItem(R.id.menu_item).icon = ColorDrawable(0x000000)
// and disable for the actions
bottomNavigation.menu.findItem(R.id.menu_item).isEnabled = false
like image 17
abdullahselek Avatar answered Oct 24 '22 03:10

abdullahselek


Try this:

navView.getMenu().findItem(R.id.your_menu_item_id).setVisible(true);
navView.getMenu().findItem(R.id.your_menu_item_id).setVisible(false);
like image 11
Mahtab Avatar answered Oct 24 '22 04:10

Mahtab