Darren Mao Darren Mao - 3 months ago 30
Java Question

Navigation drawer menu sub categories

I have a Navigation Drawer in my app using the DrawerLayout class and NavigationView. Right now I have my drawer showing all of my categories by inflating a menu xml file. How can I get it so that some of these categories are separated by subheadings and dividers?

Answer

You can do this very easily by just modifying your existing menu xml file.

Your code will look something like this:

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

    <item
        android:id="@+id/group1"
        android:title="title1">
        <menu>
            <item
                android:title="item1" />
            <item
                android:title="item2" />
        </menu>
    </item>

    <item
        android:id="@+id/group2"
        android:title="title2">
        <menu>
            <item
                android:title="item1" />
            <item
                android:title="item2" />
            <item
                android:title="item3" />
        </menu>
    </item>
</menu>

All you do is specify a new menu within each item and give the parent item a title!

Comments