22

I want to build NavigationDrawer with the possibility of adding new items (such as yahoo weather App with adding new cities). I have working NavigationDrawer with NavigationView, in menu I have permanent fields:

   <group
        android:id="@+id/group"
        android:checkableBehavior="single">
        <item
            android:checked="false"
            android:id="@+id/item1"
            android:icon="@drawable/ic_inbox_black_24dp"
            android:title="Item1" />
        <item
            android:checked="false"
            android:id="@+id/Item2"
            android:icon="@drawable/ic_inbox_black_24dp"
            android:title="Item2"
            />
    </group>

I'm trying to add new Item this method:

 public boolean addNewItem(String itemName){
        Menu menu = navigationView.getMenu();
        menu.add(R.id.group,Menu.NONE,Menu.NONE,itemName);
        return true;
 }

And I'm getting not exactly what I want: Result

Does anyone have an idea how to solve this problem? Or why is this happening? I do not know where and how one can hold information about the added elements. Any ideas?

Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28
Skye
  • 1,469
  • 3
  • 14
  • 25
  • Did you see the source of drawer? If you change something in drawer, remember to invalidate it/reload – deadfish Jul 30 '15 at 12:29
  • Ofc, the problem is that I cannot add new item below Item2. When I call method addNewItem, new item doesn't appear in the first group above the separator, but at the bottom of the menu. – Skye Jul 30 '15 at 12:40
  • what about method `addHeaderView()` ? – deadfish Jul 30 '15 at 12:49
  • @Skye have you found a workaround for this? Am having the exact same problem. – irobotxx Sep 24 '15 at 15:08
  • @manuelJ yes, when you have items with icons/images and when you try to add item dynamically, this item will be added below the current items. I also try custom libraries and it didnt work too. My solution is to write items into xml file, and after adding or deleting item (from xml file) drawer menu should be loaded again. – Skye Sep 24 '15 at 22:26

3 Answers3

31

From Check TechnoTalkative.

To add the Item programmatically, we can get a Menu object using getMenu() method of NavigationView and then we can add Items into the navigation drawer using that Menu object.

final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 3; i++) {
   menu.add("Runtime item "+ i);
}

Using SubMenu, we can add a subsection and Items into it.

// adding a section and items into it
final SubMenu subMenu = menu.addSubMenu("SubMenu Title");
for (int i = 1; i <= 2; i++) {
   subMenu.add("SubMenu Item " + i);
}

If you want to interact with the menu, use
menu.add(0, itemId, 0, title); and then

 public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

id will give you assigned itemId

Machavity
  • 30,841
  • 27
  • 92
  • 100
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
14

Suppose you have two groups, group1 and group2. If you want to dynamically add items to group1, then you can assign priority to group2 to make it always below group1. Then when you add new items to group1, it won't be inserted below group2.

Here is a sample:

<group android:id="@+id/group1" />
<group
    android:id="@+id/group2"
    android:orderInCategory="999">
    <item
        android:checked="false"
        android:id="@+id/item1"
        android:icon="@drawable/ic_inbox_black_24dp"
        android:title="Item1" />
    <item
        android:checked="false"
        android:id="@+id/Item2"
        android:icon="@drawable/ic_inbox_black_24dp"
        android:title="Item2"
        />
</group>

And when you add menu items to group1:

Menu menu = navigationView.getMenu();
menu.add(R.id.group1,Menu.NONE,Menu.NONE,itemName);

This should work. I tested it on Android design library 23.1.1.

Hexise
  • 1,520
  • 15
  • 20
2
<group android:checkableBehavior="single"> 
    <item
        android:id="@+id/send_notification"
        android:icon="@drawable/ic_menu_send"
        android:title="@string/send_notification"
        android:visible="false"/>     
</group>

if you want too add a item dynamically to a navigation drawer then make it invisible in xml and make it visible through programatically as follows.

NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu=navigationView.getMenu();
menu.findItem(R.id.send_notification).setVisible(true);

thats it.

Sanath L S
  • 1,309
  • 8
  • 10