Search code examples
javaandroidxmlandroid-studionavigation-drawer

How to add the menu in the group using the id?


I'm working on my android app to add the menu in the group using with the id. I have got a problem with adding the menu in the group because it will adding the menu on the bottom of the navigation drawer without adding the group using with the id.

Here is what it have show:

enter image description here

This is what I have tried:

menu.addSubMenu(R.id.nav_group1, 1, 100, "test1")
      .setIcon(R.drawable.ic_baseline_folder_24);
menu.addSubMenu(R.id.nav_group1, 2, 101, "test2")
      .setIcon(R.drawable.ic_baseline_folder_24);
menu.addSubMenu(R.id.nav_group1, 3, 102, "test4")
      .setIcon(R.drawable.ic_baseline_folder_24);
menu.addSubMenu(R.id.nav_group1, 4, 103, "test new folder")
      .setIcon(R.drawable.ic_baseline_folder_24);
menu.addSubMenu(R.id.nav_group1, 5, 104, "test new folder backup")
     .setIcon(R.drawable.ic_baseline_folder_24);

And I have also tried this:

menu.add(R.id.nav_group1, 1, 100, "test1")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 2, 101, "test2")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 3, 102, "test4")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 5, 104, "test new folder")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 5, 104, "test new folder backup")
     .setIcon(R.drawable.ic_baseline_folder_24);

And I have also tried this:

menu.addSubMenu(R.id.second_group, 1, 100, "test1")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.addSubMenu(R.id.second_group, 2, 101, "test2")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.addSubMenu(R.id.second_group, 3, 102, "test4")
     .setIcon(R.drawable.ic_baseline_folder_24);
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.addSubMenu(R.id.second_group, 5, 104, "test new folder backup")
     .setIcon(R.drawable.ic_baseline_folder_24);

Full code:

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
menu.add(R.id.nav_group1, 1, 100, "test1")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 2, 101, "test2")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 3, 102, "test4")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 5, 104, "test new folder")
     .setIcon(R.drawable.ic_baseline_folder_24);
menu.add(R.id.nav_group1, 5, 104, "test new folder backup")
     .setIcon(R.drawable.ic_baseline_folder_24);

activity_main_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_inbox"
            android:icon="@drawable/ic_baseline_inbox_24"
            android:title="Inbox1" />
        <item
            android:id="@+id/nav_important"
            android:icon="@drawable/ic_baseline_important_24"
            android:title="Important" />
        <item
            android:id="@+id/nav_outbox"
            android:icon="@drawable/ic_baseline_outbox_24"
            android:title="Outbox" />
        <item
            android:id="@+id/nav_sent"
            android:icon="@drawable/ic_baseline_sent_mail_24"
            android:title="Sent" />
        <item
            android:id="@+id/nav_drafts"
            android:icon="@drawable/ic_baseline_draft_24"
            android:title="Drafts" />
        <item
            android:id="@+id/nav_spam"
            android:icon="@drawable/ic_baseline_spam_24"
            android:title="Spam" />
        <item
            android:id="@+id/nav_trash"
            android:icon="@drawable/ic_baseline_trash_24"
            android:title="Trash" />
    </group>

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

    <group android:id="@+id/nav_group1">
        <item
            android:title="Folders" />
    </group>

    <group android:id="@+id/nav_group2">
        <item
            android:id="@+id/nav_create_new_folder"
            android:icon="@drawable/ic_baseline_add_24"
            android:title="Create New" />
    </group>
</menu>

What I'm trying to achieve is I want to add the menu items in the group using with the id called nav_group1.

Like this:

enter image description here

Can you please show me an example how I could add the menu into the group using with the id?


Solution

  • You'll want to use orderInCategory. You've already assigned the order to menuItems you've added programmatically.

    The problem is in your XML, when orderIncategory numbers arent assigned a number they are given a value of zero as the default. When you programmatically added the menuItems you assigned a higher orderInCategory positioning those menu items at the bottom.

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:showIn="navigation_view">
    
        <group android:checkableBehavior="single"
               android:orderInCategory="10">
            ...
        </group>
    
        <group android:visible="false" android:id="@+id/second_group" />
    
        <group android:id="@+id/nav_group1"
               android:orderInCategory="100">
            <item
                android:title="Folders" />
        </group>
    
        <group android:id="@+id/nav_group2"
               android:orderInCategory="200">
            <item
                android:id="@+id/nav_create_new_folder"
                android:icon="@drawable/ic_baseline_add_24"
                android:title="Create New" />
        </group>
    </menu>
    

    I don't have access to android stuido right know but In your java, you could also try setting inOrderNumber to zero.

    menu.addSubMenu(R.id.nav_group1, 1, 0, "test1")
          .setIcon(R.drawable.ic_baseline_folder_24);