Search code examples
androidandroiddesignsupportnavigationview

NavigationView items not checking


In my NavigationView, I have most of my items divided into sections with subheaders. I'm achieving this by putting a menu inside an item:

<?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/drawer_top"
            android:title="@string/top"/>

        <item
            android:id="@+id/drawer_header"
            android:title="@string/header1">
            <menu>
                <item
                    android:id="@+id/drawer_item1"
                    android:title="@string/item1"/>
                <item
                    android:id="@+id/drawer_item2"
                    android:title="@string/item2"/>
            </menu>
        </item>
    </group>
</menu>

However, this leads to the first item, drawer_top, being eternally checked. Other items are not checkable. My onNavigationItemSelected-Code looks like this:

@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
    menuItem.setChecked(true);

    ... (navigate)
    return true;
}

Solution

  • It seems that submenus are not checkable by design.

    Here is a workaround:

    mNavigationView.getMenu().clear();
    mNavigationView.inflateMenu(R.menu.menu_drawer);
    mNavigationView.getMenu().findItem(mNavItemId).setChecked(true);