Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android NavigationView submenus aren't added if there are some items

Tags:

android

Well, i liked an idea of NavigationView, but had bad experience with programmatically adding menu items & submenus

protected void inflateMenu(Menu menu){
    menu.addSubMenu(1, 2, 0, "Heading");
    menu.add(MAIN_GROUP_ID, 0, 0, "Something 2");
    menu.add(MAIN_GROUP_ID, 1, 0, "Title");

    menu.addSubMenu(1, 2, 0, "Sub Menu");
    menu.add(1, 3, 0, "Menu #1");

}

Sub menus isn't appearing on NavigationView, but if i remain just adding sub menus without any items, they appeared on the screen, so hence the question, how to solve this?

like image 429
Ozik Abdullaev Avatar asked Jan 18 '26 06:01

Ozik Abdullaev


2 Answers

This was fixed in v23.0.0 of the support library so don't forget to remove the workarounds as soon as you update.

like image 140
oRRs Avatar answered Jan 19 '26 18:01

oRRs


I ran into a similar problem and solved it as follows: You can notify the NavigationView that the data has changed by removing one item.

First adding an item and then deleting it afterwards does the trick for me. Call the following line after you added the Menu:

mNavigationView.getMenu().removeItem(mNavigationView.getMenu().add("").getItemId());

It's not nice but solves the problem.

like image 31
Pfennigbaum Avatar answered Jan 19 '26 18:01

Pfennigbaum