8

Right now I am trying to implement the DrawerLayout/NavigationView from the new Design Support libary (22.2.1) into my application. I already searched on the internet and especially on stackoverflow how to add a MenuItem to a Submenu with Icon and Title. I know that it is possible to add a menuitem with a title or icon.

like that:

        Menu m = mNavigationView.getMenu();
        m.add(R.id.groupID,R.id.menuItemID,orderNumber,"title");

But that is only a MenuItem with a Title,without Icon. Is it possible to add a MenuItem with a Title and Icon?

pimato
  • 89
  • 2
  • 7
  • Ok You can easily add the title and icon using xml like given answer. But if you are adding it dynamically then currently it's a bug on design support library ... http://stackoverflow.com/a/30706233/3544839 – Moinkhan Jul 25 '15 at 09:53
  • hey Moinkhan, in your stackoverflow link is nothing mentioned about the possibility to add a MenuItem with Icon and Title. So it is not possible to add SubmenuItems with Icon and Title programmatically? – pimato Jul 25 '15 at 15:17
  • If you can't add the menu item then it is not possible add sub menu too. But this bug is marked as a future release ..so its is possible in a future library.... for now it is not possible as programmatically.. – Moinkhan Jul 25 '15 at 15:34
  • Hello, i trying the same , is this bug is resolved now, is it possible now ? if yes How can i do this ? – Snehangshu Kar Oct 10 '18 at 18:54

4 Answers4

20

First get the Menu from the NavigationView:

Menu menu = mNavigationView.getMenu();

Then add your item to the menu, remember to get the return MenuItem so you can add the icon later:

MenuItem item = menu.add(groupId, menuItemId, Order, "Menu Item 1 Title");
item.setIcon(R.drawable.ic_some_menu_item_icon); // add icon with drawable resource
Grace Coder
  • 804
  • 11
  • 19
  • This code works. Developers might get confused when they don't find any method in Menu which says adding String and Drawable. But the menu.add method returns the MenuItem which enables to setIcon and other properties. – Prashant Jul 04 '17 at 11:55
3

create menu as

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
     android:id="@+id/nav_home"
     android:icon="@drawable/ic_dashboard"
     android:title="Home" />

 <item android:title="Sub items">
    <menu>
        <item
            android:icon="@drawable/ic_dashboard"
            android:title="Sub item 1" />
        <item
            android:icon="@drawable/ic_forum"
            android:title="Sub item 2" />
    </menu>
</item>
</menu>

here is sample app

https://github.com/chrisbanes/cheesesquare

sachithkn
  • 457
  • 1
  • 4
  • 13
2

For adding a menu item grammatically with icon, you need to first add a menu with id and title, using this

 menu.add( groupId, menuItemId, Order, "title" );

after this get this item using id and setIcon.

 menu.findItem( menuItemId ).setIcon( R.drawable.ic_add_black );
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0

I found easier way to change navigation view ( work with both submenu and menu). You can re-inflate NavigationViewat runtime with 2 lines of code. In this example i re-inflate with new_navigation_drawer_items.xml when user successfully logged-in

navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.logged_in_navigation_drawer_items); //inflate new items.

When user log out just re-inflate again with logged_out_navigation_drawer_items.xml

navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.logged_out_navigation_drawer_items); //inflate new items.

So it actually re-inflate items but not add new items to existed one. Just create your own menu.xml

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87