tomss tomss - 7 months ago 31
Android Question

Delete delimiter NavigationView

I made a navigation view populated by this .xml:

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


<item android:title="Menu A"
>
<menu
>
<group
android:checkableBehavior="single"
android:orderInCategory="2"
android:id="@+id/gr"
>
<item
android:id="@+id/a1"
android:title="A1"
/>

</group>

</menu>
</item>

<item android:title="Menu B"
android:orderInCategory="3"
>
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/b1"
android:title="B1"
/>
<item
android:id="@+id/b2"
android:title="B2"
/>

<item
android:id="@+id/b3"
android:title="B3"
/>
</group>
</menu>

</item>




I want to add one item programatically to my NavigationView.
I am adding that item with this line of code:

MenuItem item = menu.add(R.id.gr, Menu.NONE, 2, "A2");

Above that item (A2) appears a delimiter. I don't want that delimiter. How can I remove it?

enter image description here

Note: I don't want to put the color of my delimiters transparent. That will make all delimiters disappear.

Answer Source

Add the new item to the SubMenu, not to the Menu. So the delimiter above the item will not appear.

Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.getItem(0);
SubMenu subMenu = menuItem.getSubMenu();
subMenu.add(R.id.gr, Menu.NONE, 2, "A2");

subMenu.setGroupCheckable(R.id.gr, true, true);

Call subMenu.setGroupCheckable(R.id.gr, true, true) if you want to define the checkable behaviour for the group. This should be called after the items of the group have been added.