dynamic adding item to NavigationView in Android

android navigation drawer submenu: how to collapsible navigation items
navigation drawer with submenu in android github
custom menu item in navigation drawer android
add menu item dynamically android
how to add profile information in android navigation drawer
navigation view item click listener
android add navigation drawer to existing activity
how to add item in navigation drawer android

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:

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?

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);
}

for more details Check TechnoTalkative.

EDIT: 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

Adding Menu Items in Navigation Drawer Dynamically, If you need to create a menu item for your NavigationDrawer dynamically, then this is the best way to do it. As you First we need to initialize our NavigationView, Menu and SubMenu objects : After that we can simple add a submenu item. 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 ..

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.

Android Notes 53 : How to create Menu item for NavigationDrawer , In this post, we will progressively add things to a navigation drawer from a Now​, fire up the Android Studio, select an 'EMPTY' activity and include the Navigation View inflates navigation items from a menu resource. Adding Menu Items in Navigation Drawer Dynamically Introduction. Android Studio provides support to add Navigation Drawer Activity from IDE itself. With such Activity, it automatically generates menu items for navigation drawer in form of a xml file resource located inside res/menu directory.

<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.

How to add Dynamic Navigation Drawer Items, In this video, I will demo how to make Dynamic Navigation Drawer Items in Android To Duration: 19:09 Posted: May 7, 2017 Identify the group item and add “app:actionViewClass=android.widget.TextView” as given below: Step 2 : Declare the Navigation Drawer menu item and initialize the item with the badge value In your main Activity, declare the menu item of the Navigation Drawer as given below

Dynamic Navigation Drawer Items in Android, show dynamic menu items from server in android conventional navigation and tried to explore more about navigation view api in android. The current NavigationView is drawn over the status bar. To put it beneath the status bar set the android:fitsSystemWindows as “false” for the NavigationView. Now with the above attributes set we can further style the NavigationView by putting it under the ToolBar/ActionBar (Though this is not recommended in the Material Design Guidelines

Creating Dynamic Navigation Drawer Menu in Android , I will create a new android application by selecting Navigation Drawer activity in is the single menu item, we can divide/separate the menu by creating an another menu as I initialized the NavigationView using the following couple of lines, you can call invalidateOptionsMenu() (note:need to use compatability library like actionBarSherlock to access in case you need to support low API versions) , and then update the menu items according to the status. there you could hide the login action item and show the logout action item.

How to implement navigation drawer in your android app with an , If the destination doesn't use a DrawerLayout , the Navigation button is main content and a NavigationView for the contents of the navigation  android.R.layout.simple_list_item_1 is the default list item layout supplied by Android, and you can use this stock layout for non-complex things. listItems is a List which holds the data shown in the ListView. All the insertion and removal should be done on listItems; the changes in listItems should be reflected in the view.

Comments
  • Did you see the source of drawer? If you change something in drawer, remember to invalidate it/reload
  • 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.
  • what about method addHeaderView() ?
  • @Skye have you found a workaround for this? Am having the exact same problem.
  • @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.
  • Everything is ok but it isn't what I mean. It only works when I remove Settings and About items :/
  • How about adding icon to the item? I think thats not possible.
  • For adding an icon, menu.add() returns the menuItem, on which you can call setIcon().
  • Thanks for sharing my blog link :)
  • Menu has added but it's always added in top position, I can't add it in desire position. any Idea
  • @HiteshDhamshaniya you can still use order parameter in Menu.add() method. Larger order will be sorted in bottom.