Search code examples
androidnavigationview

Get the current Menu of navigationView as R.menu


In my app, I need to change the menu of the navigationView, when I click on the header
First state
Second state
I wanted to get the currently active menu of my navigationView to toggle between the two menus. This is what I have written so far:

public void toggleMenu(){
    navigationView.getMenu().clear();
    navigationView.inflateMenu(R.menu.menu_trips);
}

Instead of inflating the same menu all the time, I need to create an if to inflate the menu which is not currently active, something like this:

if (navigationView == R.menu.menu_trips){
   navigationView.inflateMenu(R.menu.activity_main_drawer);
} else if (navigationView == R.menu.activity_main_drawer){
   navigationView.inflateMenu(R.menu.menu_trips);
}

I do not want to add an MenuItem, I just want to replace the menu. Clearing it and then inflating is the best option I found. If you have a better solution, please tell me :)


Solution

  • You can not get menu id from Menu class. I suggest you to keep a global variable that keeps your current menu id. Or you can compare first items to understand which menu is currently active.