Search code examples
androidandroid-studiokotlinbottomnavigationviewandroid-bottomnav

Do action when holding a button from bottom navigation bar


I wanted to enter a settings page when I hold down the home button on the bottom navigation bar. I looked for how to get the id of a button in the bottom navigation but couldn't find anything.

Do you know if I can get a button from this bar?

Thanks !


Solution

  • Try this code:

    binding.bottomNav.menu.forEach {
            if (it.itemId == R.id.home) {
                binding.bottomNav.findViewById<View>(it.itemId).setOnLongClickListener {
    
                    // your action
    
                    true
                }
            }
        }
    

    It iterates through all the menu items of the BottomNavView and sets LongClickListener for the desired menu item