Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a group to navigation drawer programmatically

I have a navigation drawer in my android app. I can add groups and items to it by mean of XML, however, I need to add new groups to it programmatically.

For example, I have this XML:

<group android:checkableBehavior="single">
    <item android:id="@+id/nav_camara" android:icon="@android:drawable/ic_menu_camera"
        android:title="Import" />
    <item android:id="@+id/nav_gallery" android:icon="@android:drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item android:id="@+id/nav_slideshow" android:icon="@android:drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item android:id="@+id/nav_manage" android:icon="@android:drawable/ic_menu_manage"
        android:title="Tools" />
</group>

How can I do it if that group has no items and I need to add them by code?

If I use:

Menu sistemas = navigationView.getMenu();
sistemas.add(Menu.FIRST, 1, 0, "Prueba");

The item is added as a menu below all options, not as a group.

Regards, Jaime

like image 513
jstuardo Avatar asked Oct 14 '15 15:10

jstuardo


1 Answers

My best suggestion if you want to stick to NavigationView is to do the following:

Set up your XML to contain any groups you think you need to add dynamically and set them to invisible:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <group android:id="@+id/main_group">
         <item android:id="@+id/nav_camara"
             android:icon="@android:drawable/ic_menu_camera"
             android:title="Import" />
         <item android:id="@+id/nav_gallery" 
             android:icon="@android:drawable/ic_menu_gallery"
             android:title="Gallery" />
         <item android:id="@+id/nav_slideshow" 
             android:icon="@android:drawable/ic_menu_slideshow"
             android:title="Slideshow" />
         <item android:id="@+id/nav_manage" 
             android:icon="@android:drawable/ic_menu_manage"
             android:title="Tools" />
    </group>
    <group android:visible="false" android:id="@+id/second_group">

    </group>
    <group android:visible="false" android:id="@+id/third_group">

    </group>
</menu>

Then when you need to show them or add dynamic options for these groups:

      Menu menu = mNavView.getMenu();

      // Add items to the second group, and set to visible
      menu.add(R.id.second_group, 1, 100, "Item 1");
      menu.add(R.id.second_group, 2, 200, "Item 2");
      menu.add(R.id.second_group, 3, 300, "Item 3");
      menu.setGroupCheckable(R.id.second_group, true, true);
      menu.setGroupVisible(R.id.second_group, true);

      // Add items to the third group, and set to visible
      menu.add(R.id.third_group, 4, 400, "Item 1");
      menu.add(R.id.third_group, 5, 500, "Item 2");
      menu.add(R.id.third_group, 6, 600, "Item 3");
      menu.setGroupCheckable(R.id.third_group, true, true);
      menu.setGroupVisible(R.id.third_group, true);

Just make sure when you add your items, the item Id's are different and the order in category are the actual order in the whole menu of dynamic items.

like image 75
kandroidj Avatar answered Oct 11 '22 13:10

kandroidj