How to programmatically add a submenu item to the new material design android support library

[Update 20-03-2016]
Bug is resolved. So no need to worry.

[This below contetnt is outdated.]

Adding a dynamic menu to NavigationView is currently bug on Design Support library. And I have report it to android bug source tracking. So wait till the bug will fixed. But if you want the temporary solution you can do it. First add your dynamic menu ..

    navView = (NavigationView) findViewById(R.id.navView);
    Menu m = navView.getMenu();
    SubMenu topChannelMenu = m.addSubMenu("Top Channels");
    topChannelMenu.add("Foo");
    topChannelMenu.add("Bar");
    topChannelMenu.add("Baz");

After adding your menu just write below code ..

    MenuItem mi = m.getItem(m.size()-1);
    mi.setTitle(mi.getTitle());

It’s currently hacky solution. But work for me …

[update 26-06-2015]

As I have reported this bug at Android Bug source Now Bug is marked as a future release, here is the link https://code.google.com/p/android/issues/detail?id=176300

So we can say that the bug is no more exist on future library. So you don’t have to use tricky solution. i will also update this answer again when Future version release number is maintion for this bug.

Leave a Comment