Skip to content Skip to sidebar Skip to footer

How To Change A Menu Of A Navigationview Programmatically?

Hello I'm wondering how can I change a Menu ID from a NavigationView, in this case it is triggered when the user clicks on a RadioButton. I have that part already but I just need t

Solution 1:

In java you could try this to re-inflate NavigationView at runtime.

navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.new_navigation_drawer_items);

I don't know kotlin,but the thought is same.

Solution 2:

this code work for me as explain by navylover answer 1- make sure you have different menu in my case i have R.menu.navigation and R.menu.navigation_normal

privatefunUser() {
                // clear previous menu
                binding.navi.menu.clear()

               // add new menu item
                binding.navi.inflateMenu(R.menu.navigation)

              //add listener for new bottom naviagtion 

       binding.navi.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

                 // when using Navigation component//set up bottom navigation bar with navigation graph 
                NavigationUI.setupWithNavController(binding.navi, navController)
            }

Post a Comment for "How To Change A Menu Of A Navigationview Programmatically?"