6

I am trying to add menu items to menu group pragmatically but I found no way to do that. I am using Navigation View and added below mentioned menu:

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

    <item
        android:id="@+id/nav_lang_currency"
        android:title="" />

    <item
        android:id="@+id/nav_home"
        android:title="" />

    <group android:id="@+id/nav_refer" />

    <item
        android:id="@+id/nav_setting"
        android:title="" />

    <item
        android:id="@+id/nav_about_us"
        android:title="" />

    <item
        android:id="@+id/nav_logout"
        android:title="" />

</menu>

Everything looks good as mentioned.

I just want to add multiple menu items in nav_refer group at run-time as per business requirement but I found no way to do that.

I searched solution on SO but found no way to do that.

Kindly suggest me how to add multiple menu items in group at run-time.

ekad
  • 14,436
  • 26
  • 44
  • 46
Geeky Singh
  • 1,003
  • 9
  • 20

3 Answers3

11

To add menu to a particular group, call this method Menu.add(int groupId, int itemId, int order, CharSequence title)

    Menu menu = navigationView.getMenu();
    menu.add(R.id.nav_refer, 123, Menu.NONE, "Title1");
    menu.add(R.id.nav_refer, 124, Menu.NONE, "Title2");
    menu.add(R.id.nav_refer, 125, Menu.NONE, "Title3");

Important : Initially if you have empty group then newly added items will appear in bottom, to solve this you need to mention orders for groups. add a attribute for all your groups android:orderInCategory="101"

Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • I am also facing a similar issue but the above solution does not work for me. Where does 101 come from? – Ankit Batra Nov 21 '18 at 21:46
  • @AnkitBatra please visit https://stackoverflow.com/questions/28715765/what-is-orderincategory-in-actionbar-menu-item-why-it-is-use-for for `android:orderInCategory` attribute. – Bharatesh Nov 26 '18 at 05:52
  • Thanks, @Bharatesh. I have solved the issue I was facing. – Ankit Batra Nov 26 '18 at 14:53
  • well in that case items will be in the top – user25 Nov 01 '20 at 11:21
  • e.g. you have 3 and you need to add items to the second group, then set 100 order for first group, 101 for second, 102 for thirst and then add items to second group using `menu.add(R.id.nav_refer, ITEM_ID, 101, ITEM_TITLE);` – user25 Nov 01 '20 at 11:25
1

You can do something like this:

NavigationView navView = (NavigationView) findViewById(R.id.navView);
Menu menu = navView.getMenu();
SubMenu subMenu = menu.addSubMenu("sub menu");
subMenu.add("item 1");
subMenu.add("item 2");
subMenu.add("item 3");
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
-2

create res --> menu file new layout

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <group android:checkableBehavior="single">
        <item android:title="حساب کاربری">
            <menu>
                <group android:title="حساب کاربری">
                    <item android:title="ورود" />
                    <item android:title="ثبت نام" />
                </group>
            </menu>
        </item>
    </group>
    <group android:checkableBehavior="single">
        <item android:title="سایت">
            <menu>
                <group android:title="حساب کاربری">
                    <item android:title="ورود" />
                    <item android:title="ثبت نام" />
                </group>
            </menu>
        </item>
    </group>
</menu>

set NavigationView to app:menu="@menu/drawer_view"

i hope help you

hamid
  • 171
  • 1
  • 1
  • 9