How to add an item to a menu group in NavigationView How to add an item to a menu group in NavigationView android android

How to add an item to a menu group in NavigationView


On checking MenuItemImpl source code

     ...     *    @param group Item ordering grouping control. The item will be added after     *            all other items whose order is <= this number, and before any     *            that are larger than it. This can also be used to define     *            groups of items for batch state changes. Normally use 0.     ...    MenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering,        CharSequence title, int showAsAction) {

So you should define ordering in your xml (give same order to items in one group and increment in each following group)

<menu xmlns:android="http://schemas.android.com/apk/res/android">    <group android:id="@+id/my_move" android:checkableBehavior="single">        <item            android:orderInCategory="0"            android:id="@+id/game1"            android:icon="@drawable/ic_stars_black_24dp"            android:title="Game #1" />        <item            android:orderInCategory="0"            android:id="@+id/game2"            android:icon="@drawable/ic_stars_black_24dp"            android:title="Game #2" />    </group>    <group android:id="@+id/his_move" android:checkableBehavior="single">        <item            android:orderInCategory="1"            android:id="@+id/game5"            android:icon="@drawable/ic_clock_black_24dp"            android:title="Game #5" />        <item            android:orderInCategory="1"            android:id="@+id/game6"            android:icon="@drawable/ic_clock_black_24dp"            android:title="Game #6" />        <item            android:orderInCategory="1"            android:id="@+id/game7"            android:icon="@drawable/ic_clock_black_24dp"            android:title="Game #7" />    </group>    .....</menu>

and give an appropriate order value while adding the item in your code. So if you want to add the item at the end of first group, add it as:

menu.add(R.id.my_move, Menu.NONE, 0, "Item1");

and if you want to add to second group, add it as:

menu.add(R.id.his_move, Menu.NONE, 1, "Item2");

The problem with your code could be that all items in the xml have default orderInCategory 0 and so the new item gets added afterall these items.

UPDATE

To add icon use setIcon method for MenuItem

menu.add(R.id.my_move, Menu.NONE, 0, "Item1").setIcon(R.drawable.ic_stars_black_24dp);


enter image description here

I've solved it this way:

  1. Set up the menu:

    <?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:title="my moves"          android:id="@+id/submenu_1">        <menu>            <item                android:id="@+id/my_dummy_item_1"                android:icon="@drawable/ic_menu_camera"                android:title="Import" />            <item                android:id="@+id/my_dummy_item_2"                android:icon="@drawable/ic_menu_gallery"                android:title="Gallery" />            <item                android:id="@+id/add_item"                android:icon="@drawable/ic_menu_manage"                android:title="Add Item" />        </menu>    </item>    <item android:title="opponent's moves"          android:id="@+id/submenu_2">        <menu>            <item                android:id="@+id/opponent_dummy_item_1"                android:icon="@drawable/ic_menu_camera"                android:title="Import" />            <item                android:id="@+id/opponent_dummy_item_2"                android:icon="@drawable/ic_menu_gallery"                android:title="Gallery" />            <item                android:id="@+id/opponent_dummy_item_3"                android:icon="@drawable/ic_menu_manage"                android:title="Tools" />        </menu>    </item></menu>
  2. In onNavigationItemSelected(), get MenuItem you want to expand by order id (or via findItem()), then get SubMenu from it and add new item into it:

    @SuppressWarnings("StatementWithEmptyBody")@Overridepublic boolean onNavigationItemSelected(MenuItem item) {    int id = item.getItemId();    if (id == R.id.add_item) {        Random r = new Random();        int i = r.nextInt(100);        MenuItem myMoveGroupItem = navigationView.getMenu().getItem(0);        // MenuItem myMoveGroupItem = navigationView.getMenu().findItem(R.id.submenu_1);  -- it also works!        SubMenu subMenu = myMoveGroupItem.getSubMenu();        subMenu.add("Item "+i);    }    return true;}

I hope, it helps


menu.add(R.id.my_move, i, i, "Item " + i);

You are also assigning the order (3rd param) as i. I am guessing that this is overriding the groupId. Try setting it as NONE as mentioned here

menu.add(R.id.my_move, i, NONE, "Item " + i);

Edit:Maybe something like this

MenuItem lastItem = menu.findItem(R.id.<lastItemId>);int lastOrder= lastItem.getOrder();menu.add(R.id.my_move, i, lastOrder-5, "Item " + i);

Order is a combination of category and order, so it might not be as straight forward as this.