7

I need to have a DrawerMenu with programmatically generated items, I can add the menuItems but only with a title and an icon, I want them to have a subtitle also (Like in the picture)

enter image description here

I get the Drawer by its ID, then its menu and then I add MenuItems like this (Code snippet from my MainActivity):

int id =0;
String MenuTitle = "Title of the item in menu";
NavigationView mDrawerList = (NavigationView)findViewByID(R.id.nav_view);
Menu menu = mDrawerList.getMenu();
MenuItem menuItem;
menuItem = menu.add (Menu.NONE, id, Menu.NONE, title);
menuItem.setIcon(R.drawable.ic_action_home);
menuItem.setCheckable(true);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Enrique Alcazar
  • 381
  • 7
  • 21
  • Possible duplicate of [How can I dynamically create menu items?](http://stackoverflow.com/questions/15580111/how-can-i-dynamically-create-menu-items) – Karan Rana Feb 23 '16 at 12:53
  • @KaranRana Nope, I have already done that, I'm asking how to add SUBTITLES specifically. In the code I explained how I did it – Enrique Alcazar Feb 23 '16 at 12:58
  • would you like to create listview and insert it in your drawer? if yes let me know to show you an example – Mounir Elfassi Apr 23 '16 at 21:14

3 Answers3

7

Currently NavigationView doesn't allow to add subTitle to it's menu.

But if you want to achieve this I would recommend you take the ListView Nested inside the NavigationView. So that you can customize your ListView cell accrding to your need.

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/nav_header" />

        <ListView
            android:id="@+id/lst_menu_items"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</android.support.design.widget.NavigationView>

Note: Don't forget to remove app:menu from NavigationView if you are using ListView and Menu file both at the same time. it will overlap your ListView on the menu items.

Moinkhan
  • 12,732
  • 5
  • 48
  • 65
0

You should override the onPrepareOptionsMenu() method in your activity and add the items you want.

Example Code

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();

    menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);

    return super.onPrepareOptionsMenu(menu);
}
Farhad
  • 12,178
  • 5
  • 32
  • 60
  • That would just create a new menu item, not add the option to add a subtitle to the constructor of the menu right? Thanks for the answer – Enrique Alcazar Feb 24 '16 at 08:02
-1

For anyone needs to change the options of the menu dynamically:

private Menu menu;

// ...

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  this.menu = menu;
  getMenuInflater().inflate(R.menu.options, menu);
  return true;
}

// ...

private void hideOption(int id)
{
  MenuItem item = menu.findItem(id);
  item.setVisible(false);
}

private void showOption(int id)
{
  MenuItem item = menu.findItem(id);
  item.setVisible(true);
}

private void setOptionTitle(int id, String title)
{
  MenuItem item = menu.findItem(id);
  item.setTitle(title);
}

private void setOptionIcon(int id, int iconRes)
{
  MenuItem item = menu.findItem(id);
  item.setIcon(iconRes);
}
Nirmal Shethwala
  • 268
  • 1
  • 15