Search code examples
androidandroid-navigationview

NavigationView menu item with heading issue


Inside my NavigationView menu I have these items. There are some items with heading which is having some issues I think because when I used android:checkableBehavior it's not affected or doesn't show that it's selected or being highlighted. The items highlighted are only the nav1, nav2, nav3 items on the menu for the Drawer Layout. What I wanted to achieve is when the user clicks on the items with some headings on it like the nav4, or nav5 item It should get highlighted when selected.

    <group android:checkableBehavior="single">
        <item android:id="@+id/nav_1" android:icon="@drawable/ic_1"
            android:title="@string/nav1" />
        <item android:id="@+id/nav_2" android:icon="@drawable/ic_2"
            android:title="@string/nav2" />
        <item android:id="@+id/nav_3" android:icon="@drawable/ic_3"
            android:title="@string/nav3" />

        <item android:title="@string/heading1">
            <menu >
                <item android:id="@+id/nav_4" android:icon="@drawable/ic_4"
                    android:title="@string/nav4"/>
                <item android:id="@+id/nav_5" android:icon="@drawable/ic_5"
                    android:title="@string/nav5" />
            </menu>
        </item>
    </group>

Solution

  • if you want nav4 and nav5 to get highlighted too change to this:

    <group android:checkableBehavior="single">
        <item android:id="@+id/nav_1" android:icon="@drawable/ic_1"
            android:title="@string/nav1" />
        <item android:id="@+id/nav_2" android:icon="@drawable/ic_2"
            android:title="@string/nav2" />
        <item android:id="@+id/nav_3" android:icon="@drawable/ic_3"
            android:title="@string/nav3" />
    
        <item android:title="@string/heading1">
            <menu >
                <group android:checkableBehavior="single">
                    <item android:id="@+id/nav_4" android:icon="@drawable/ic_4"
                    android:title="@string/nav4"/>
                    <item android:id="@+id/nav_5" android:icon="@drawable/ic_5"
                    android:title="@string/nav5" />
                </group>
            </menu>
        </item>
    </group>