How to Dynamically Hide a Menu Item in Bottomnavigationview

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.

How to hide and show a menu in BottomNavigationView?

    bottomNavigation.getMenu().removeItem(R.id.nav_user_download);

removeItem(int menu_item_id),call this method.

i have try hide/show method @Sachin Rao, but it work not very well. so i finally found this way, it's work well for me.

How to disable menu item of bottom navigation view?

You should get the menu of your BottomNavigationView and then find and disable the MenuItem that you want. In code it could be done as follows

override fun onCreate(savedInstanceState: Bundle?) {
// Find the bottom navigation view, (Use correct ID)
// menu_item_1 is probably not a good ID for a navigation view
val navView: BottomNavigationView = findViewById(R.id.nav_view)

// Find the menu item and then disable it
navView.menu.findItem(R.id.navigation_home).isEnabled = false
}

Hide or remove static menu item in Android

I found solution here

How to dynamically hide a menu item in BottomNavigationView?

   @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

// This is a way to hide static menu item
navigation.getMenu().removeItem(R.id.navigation_home);
...
}

how to hide BottomNavigationView on android-navigation lib

You could do something like this in your activity's onCreate. When ever an item in the nav bar is selected it will show or hide the nav based on the fragment id's.

private fun setupNav() {
val navController = findNavController(R.id.nav_host_fragment)
findViewById<BottomNavigationView>(R.id.bottomNav)
.setupWithNavController(navController)

navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.mainFragment -> showBottomNav()
R.id.mineFragment -> showBottomNav()
else -> hideBottomNav()
}
}
}

private fun showBottomNav() {
bottomNav.visibility = View.VISIBLE

}

private fun hideBottomNav() {
bottomNav.visibility = View.GONE

}

Menu item visibility doesnt change partly

In order to make sure that mAuth.currentUser is null, you need to add a listener when the user has logged out so that you can update visibility of menu items; because you might call updateUI() before the user sign out, so mAuth.currentUser still has a value.

So in onNavigationItemSelected() callback: add this listener

R.id.nav_logout_item->{

mAuth.signOut()

AuthUI.getInstance()
.signOut(this) // context
.addOnCompleteListener {

// User has been logged out
updateUI()

}
}

And do the same for the login behavior to return the visibility back to the menu items.

Android - Add bottom navigation view dynamically

This is a bug of BottomNavigationView.

Here is the bug reference: https://issuetracker.google.com/issues/37124043

This has been fixed in support library 25.0.1. Update your support library and try again.

Hope this will help~



Related Topics



Leave a reply



Submit