Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add MenuItem to NavigationView with Icon and Title?

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?

like image 909
pimato Avatar asked Jul 24 '15 20:07

pimato


2 Answers

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
like image 191
Grace Coder Avatar answered Oct 15 '22 18:10

Grace Coder


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

like image 45
sachithkn Avatar answered Oct 15 '22 18:10

sachithkn