0

In my App I have a sliding menu.

My activity.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/sliding_menu_options" />
</android.support.v4.widget.DrawerLayout>

The sliding_menu_options.xml looks like this:

<?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/nav_home"
            android:icon="@drawable/ic_home_black"
            android:title="@string/home"/>
        <item
            android:id="@+id/nav_recently"
            android:icon="@drawable/ic_file_upload"
            android:title="@string/recent"/>
    </group>

    <group
        android:checkableBehavior="single">
        <item
            android:id="@+id/otherItem"
            android:title="Other">
            <menu>
                <item
                    android:id="@+id/nav_about_us"
                    android:icon="@drawable/ic_info_black"
                    android:title="@string/about"/>
                <item
                    android:id="@+id/nav_licences"
                    android:icon="@drawable/ic_description_black"
                    android:title="@string/licences"/>
            </menu>
        </item>
    </group>
</menu>

This picture shows the tutorial I used for my slidin menu and how the two groups should roughly look like. Other should be something like the headline or description for the second group:

enter image description here

When the user selects one of the four options, I want to mark the option in the sliding menu as checked. For the first two options it works fine with this piece of code:

navigationView = (NavigationView) findViewById(R.id.nav_view);
 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

               navigationView.getMenu().findItem(menuItem.getItemId()).setChecked(true);

                return true;
            }
        });

Unfortuantely the Menu withing NavigationView contains only two Items (nav_home and nav_recently) instead of all four. So it does not work for nav_about_us and nav_licences to mark them as checked.

How can I check the other two options while still having two groups?

joshi737
  • 869
  • 3
  • 12
  • 26

1 Answers1

1

Looks ike there is a problem in sliding_menu_options.xml ,try following modified code .

    <?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/nav_home"
            android:icon="@drawable/ic_home_black"
            android:title="@string/home"/>
        <item
            android:id="@+id/nav_recently"
            android:icon="@drawable/ic_file_upload"
            android:title="@string/recent"/>
    </group>

<item
            android:id="@+id/otherItem"
            android:title="Other">
</item>

    <group
        android:checkableBehavior="single">

                <item
                    android:id="@+id/nav_about_us"
                    android:icon="@drawable/ic_info_black"
                    android:title="@string/about"/>
                <item
                    android:id="@+id/nav_licences"
                    android:icon="@drawable/ic_description_black"
                    android:title="@string/licences"/>


    </group>
</menu>
Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41