Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationView: how to insert divider without subgroup?

How I can put a divider without title Subgroup in the new NavigationView?

<?xml version="1.0" encoding="utf-8"?>

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


<group android:checkableBehavior="single">
    <item
        android:id="@+id/drawer_armario"
        android:icon="@drawable/armario"
        android:title="@string/armario"  />
    <item
        android:id="@+id/drawer_amigos"
        android:icon="@drawable/amigos"
        android:title="@string/amigos" />

</group>

<item android:title="Configuración">
    <menu>
        <item

            android:id="@+id/drawer_ajustes"
            android:icon="@drawable/ajustes"
            android:title="@string/ajustes" />
        <item
            android:id="@+id/drawer_ayuda"
            android:icon="@drawable/ayuda"
            android:title="@string/ayuda" />
        <item
            android:id="@+id/drawer_sign_out"
            android:icon="@drawable/sign_out"
            android:title="@string/sign_out" />

    </menu>

</item>

I would like to put a line beetween amigos and ajustes items without Configuración title.

Thanks!!!

like image 392
Android-developer Avatar asked Jun 02 '15 19:06

Android-developer


2 Answers

All you need to do is define a group with an unique ID. I looked through NavigationView's implementation, and it will create a divider every time the group id is changed.

e.g.

<group android:id="@+id/my_id">
    <!-- Divider will appear above this item -->
    <item ... />
</group>
like image 80
Jin Avatar answered Oct 19 '22 04:10

Jin


I tried this and it worked

See implementation from line no 122-125

This is my menu file and it is creating the separator:

<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=".MyFirstMNCActivity">
    <group android:id="@+id/grp1">
        <item
            android:id="@+id/navigation_item_1"
            android:checked="true"
            android:icon="@drawable/ic_home"
            android:title="@string/navigation_item_1" />

    </group>

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

        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/ic_home"
            android:title="@string/navigation_item_2" />
    </group>
</menu>
like image 6
N J Avatar answered Oct 19 '22 05:10

N J