I am using the new NavigationView
to create my navigation drawer menu from XML. I need to place a divider between the section menu items, which switch between the sections of my app, and the settings and help & support links at the bottom.
In all the examples I've seen, I see how this can be done by putting another <menu>
within an <item>
, but the <item>
requires to have the android:title
attribute, so the best I can do is make the title blank, which leaves an empty space before the settings and help & feedback.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_section_1"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_1"
android:checked="true" /> <!-- default selection -->
<item
android:id="@+id/nav_section_2"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_2" />
<item
android:id="@+id/nav_section_3"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_3" />
</group>
<item android:title="@null"> <!-- I don't want a title or space here! -->
<menu>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" />
<item
android:id="@+id/nav_help_feedback"
android:icon="@drawable/ic_help"
android:title="@string/help_feedback" />
</menu>
</item>
</menu>
I've tried various combinations of <menu>
, <item>
and <group>
tags, but haven't found anything that will work. This for example has the issue of using the last item in the previous group as the group title:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_section_1"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_1"
android:checked="true" /> <!-- default selection -->
<item
android:id="@+id/nav_section_2"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_2" />
<item
android:id="@+id/nav_section_3"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_3" />
</group>
<group> <!-- This puts @string/section_3 as the group title! -->
<menu>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" />
<item
android:id="@+id/nav_help_feedback"
android:icon="@drawable/ic_help"
android:title="@string/help_feedback" />
</menu>
</item>
</menu>
There just has to be an easy way to do this using just the menu XML description. Google has this very behavior in their Material design spec.
EDIT:
Yet another close attempt:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="@null"> <!-- Still a space here though! -->
<menu>
<group android:checkableBehavior="single"> <!-- And this checkable behavior behaves strangely for some reason -->
<item
android:id="@+id/nav_section_1"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_1"
android:checked="true" /> <!-- default selection -->
<item
android:id="@+id/nav_section_2"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_2" />
<item
android:id="@+id/nav_section_3"
android:icon="@drawable/ic_dashboard"
android:title="@string/section_3" />
</group>
</menu>
</item>
<group> <!-- Finally, no space or title here! -->
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" />
<item
android:id="@+id/nav_help_feedback"
android:icon="@drawable/ic_help"
android:title="@string/help_feedback" />
</item>
</menu>
This leaves no space between the items above and below the divider, but there's still the space at the top now. Also, the android:checkableBehavior="single"
behaves strangely. Items are not selected when selected the first time and items are not unselected once others do become selected.
From: NavigationView: how to insert divider without subgroup?
It looks like you just need to give your
group
tags unique ID's.As the answer says: