Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dividers in NavigationView menu without titles?

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.

Settings and support

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.

like image 244
Jeff Lockhart Avatar asked Jun 11 '15 20:06

Jeff Lockhart


2 Answers

From: NavigationView: how to insert divider without subgroup?

It looks like you just need to give your group tags unique ID's.

<group android:id="@+id/my_id">
    <!-- Divider will appear above this item -->
    <item ... />
</group>

As the answer says:

[NavigationView] will create a divider every time the group id is changed

like image 174
Kenny Worden Avatar answered Nov 04 '22 06:11

Kenny Worden


This is exact solution for your question here.

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

    <group
        android:id="@+id/menu_top"
        android:checkableBehavior="single">
        <item android:title="Switch Team">
            <menu>
                <item
                    android:id="@+id/team"
                    android:title=""
                    app:actionLayout="@layout/layout_spinner_for_drawer"/>
            </menu>
        </item>
    </group>

    <group
        android:id="@+id/menu_bottom"
        android:checkableBehavior="single">
            <item
                android:id="@+id/nav_home"
                android:icon="@drawable/home"
                android:title="Home" />
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/sharebox"
                android:title="Sharebox" />
            <item
                android:id="@+id/nav_recognize"
                android:icon="@drawable/recognize"
                android:title="Recognize" />
            <item
                android:id="@+id/nav_contact_us"
                android:icon="@drawable/contactus"
                android:title="Contact Us" />
            <item
                android:id="@+id/nav_logout"
                android:icon="@drawable/signout"
                android:title="Logout" />
    </group>
</menu>
like image 41
Sagar Yadav Avatar answered Nov 04 '22 05:11

Sagar Yadav