Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am unable to set a submenu item as checked

I am using a NavigationView for my drawer. This is the menus.xml file which defines all the menu items for the drawer:

<?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/item_1"
            android:title="Item 1"
            android:checked="true" />
        <item
            android:id="@+id/item_2"
            android:title="Item 2" />
        <item
            android:id="@+id/item_3"
            android:title="Item 3" />
        <item
            android:id="@+id/item_4"
            android:title="Item 4" />
        <item android:title="List 1">
            <menu>
                <item
                    android:id="@+id/subitem_1"
                    android:title="SubItem 1" />
                <item
                    android:id="@+id/subitem_2"
                    android:title="SubItem 2" />
            </menu>
        </item>
        <item android:title="List 2">
            <menu>
                <item
                    android:id="@+id/subitem_3"
                    android:title="SubItem 3" />
                <item
                    android:id="@+id/subitem_4"
                    android:title="SubItem 4" />
            </menu>
        </item>
    </group>
</menu>

Thing is when I call setChecked(true) on the sub menu items it doesn't get highlighted.

So if I force the attribute android:checkable="true" on the sub-menu items, it takes two clicks to have it highlighted. Also, as you can see Item 1 is highlighted and the checkableBehaviour is set to single. In this case, the sub menu and the first item remain highlighted. Why is the sub menu not getting highlighted on running setChecked(true)?

like image 621
Abhishek Dabholkar Avatar asked Nov 30 '22 17:11

Abhishek Dabholkar


1 Answers

For submenu should be added the group with android:checkableBehavior="single"

Your xml should be something like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/item_1"
        android:checked="true"
        android:title="Item 1" />
    <item
        android:id="@+id/item_2"
        android:title="Item 2" />
    <item
        android:id="@+id/item_3"
        android:title="Item 3" />
    <item
        android:id="@+id/item_4"
        android:title="Item 4" />
    <item android:title="List 1">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/subitem_1"
                    android:title="SubItem 1" />
                <item
                    android:id="@+id/subitem_2"
                    android:title="SubItem 2" />
            </group>
        </menu>
    </item>
    <item android:title="List 2">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/subitem_3"
                    android:title="SubItem 3" />
                <item
                    android:id="@+id/subitem_4"
                    android:title="SubItem 4" />
            </group>
        </menu>
    </item>
</group>

like image 176
jlga Avatar answered Dec 03 '22 05:12

jlga