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();
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.
mBottomNavigationView.getMenu().removeItem(R.id.item_name);
removeItem does the trick. Not sure why setVisible method is not working.
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
Try this:
navView.getMenu().findItem(R.id.your_menu_item_id).setVisible(true);
navView.getMenu().findItem(R.id.your_menu_item_id).setVisible(false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With