Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep single checkableBehavior mode in drawer menu for NavigationView when we add section?

I try to implement a drawer with new component of material design : NavigationView.

It's work very well. When I select an item changes its color change well with android:checkableBehavior="single".

<group
    android:checkableBehavior="single">

    <item
        android:id="@+id/drawer_home"
        android:checked="true"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/home"/>

    <item
        android:id="@+id/drawer_favourite"
        android:icon="@drawable/ic_favorite_black_24dp"
        android:title="@string/favourite"/>
    ...

    <item
        android:id="@+id/drawer_settings"
        android:icon="@drawable/ic_settings_black_24dp"
        android:title="@string/settings"/>

</group>

The problem come when I try to use section in drawer. It's this case, I can't use android:checkableBehavior="single" and I lost the color change in the selection of an item.

<item
    android:id="@+id/section"
    android:title="@string/section_title">

    <menu>
        <item
            android:id="@+id/drawer_favourite"
            android:icon="@drawable/ic_favorite_black_24dp"
            android:title="@string/favourite"/>

        <item
            android:id="@+id/drawer_downloaded"
            android:icon="@drawable/ic_file_download_black_24dp"
            android:title="@string/downloaded"/>
    </menu>

</item>
like image 312
lopez.mikhael Avatar asked Jun 25 '15 08:06

lopez.mikhael


People also ask

How to add new fragment in navigation drawer in android studio?

You just create new Fragment class which extend Fragment and their respective XML file and call them in MainActivity eg. Show activity on this post. In android studio 3.5 and above, this comes by default. You will notice different fragment are generated by default.

How to design navigation drawer in android studio?

The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.


1 Answers

try this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">

        <item
            android:id="@+id/drawer_home"
            android:checked="true"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="@string/home"/>

       <item
            android:id="@+id/drawer_favourite"
            android:icon="@drawable/ic_favorite_black_24dp"
            android:title="@string/favourite"/>
       ...

       <item
            android:id="@+id/drawer_settings"
            android:icon="@drawable/ic_settings_black_24dp"
            android:title="@string/settings"/>
       <item
            android:id="@+id/section"
            android:title="@string/section_title">

            <menu>
                <group android:checkableBehavior="single">
                    <item
                        android:id="@+id/drawer_favourite"
                        android:icon="@drawable/ic_favorite_black_24dp"
                        android:title="@string/favourite"/>

                    <item
                        android:id="@+id/drawer_downloaded"
                        android:icon="@drawable/ic_file_download_black_24dp"
                        android:title="@string/downloaded"/>
                </group>
            </menu>

       </item>

    </group>
</menu>

you can check this solution for details.. I am unable to set a submenu item as checked

like image 158
abidsplanet Avatar answered Oct 13 '22 11:10

abidsplanet