NavigationView: how to insert divider without subgroup?
Asked Answered
K

4

22

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!!!

Kingston answered 2/6, 2015 at 19:48 Comment(1)
Use Jin's answer. It works.Slesvig
J
58

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>
Jin answered 2/6, 2015 at 20:25 Comment(5)
Ooooh, I need to try this! I hope it works because this has been bugging me. I'll test it out and report back!!Slesvig
Set <dimen name="navigation_separator_vertical_padding">0dp</dimen> to remove the padding when you do this unless you want each group to have padding.Cruck
@DanBrough how do you use the vertical specify in menu item ? by defining <dimen name="navigation_separator_vertical_padding">0dp</dimen>N
@DanBrough how does that help removing padding of divider. :\ can you review your coment?Deity
Its now called "design_navigation_separator_vertical_padding". Read about it here: #30585054Cruck
K
6

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>
Kirkkirkcaldy answered 4/6, 2015 at 9:20 Comment(0)
W
6
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
NavigationMenuView navMenuView = (NavigationMenuView) navigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(appContext,DividerItemDecoration.VERTICAL_LIST));

Here is the gist of DividerItemDecoration

DividerItemDecoration.java

Whitsuntide answered 14/2, 2016 at 18:14 Comment(3)
Can I change the height of addItemDecoration line to 1px?Aerify
yes, you can update it in DividerItemDecoration.i guess it is already 1px. if not update it to 1px;Whitsuntide
How? Is there any method to do so? addItemDecoration doesn't accept any parameter which decides the size of the lineAerify
A
3

Using xml

<android.support.design.widget.NavigationView
...
app:itemIconTint="@android:color/black" 
... />

Programmatically

 yourNavigationView.setItemIconTintList(null);

I feel that is good answer.

Best Luck.

Adherence answered 8/1, 2016 at 4:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.