Dushyant Suthar Dushyant Suthar - 7 months ago 22
Java Question

Implementing multiple selection accross multiple groups in Navigation Drawer

I want to have this kind of selection in my

Navigation Drawer
. This picture shows the selection which are made initially (by default not by user).

enter image description here

I have implemented this by the following code

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Category">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/coaching"
android:icon="@drawable/coaching"
android:title="Coaching"
android:checked="true"/>
<item
android:id="@+id/training"
android:icon="@drawable/training"
android:title="Training"/>
</group>
</menu>
</item>

<group android:checkableBehavior="single">
<item
android:id="@+id/new_registrations"
android:icon="@drawable/new_registrations"
android:title="New Registrations"
android:checked="true"/>
<item
android:id="@+id/ready_certificates"
android:icon="@drawable/ready_certificates"
android:title="Certificates Ready To Collect"/>
<item
android:id="@+id/allotted_certificates"
android:icon="@drawable/allotted_certificates"
android:title="Certificates Allotted So Far"/>
</group>




But the thing is when I select any item manually then all the previously selected items from both groups get deselected. So I want to validate one selection from group one and one selection from group two. Looking for missing attributes.

Answer Source
  • Remove android:checkableBehavior="single" from both groups.
  • Remove android:checked="true" from both "coaching" and "new_registrations" items.
  • Set android:checkable="true" for each item individually.
  • Set unique ids for both groups ("@+id/first_group" and "@+id/second_group").

Your activity_main_drawer.xml should look like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Category">
        <menu>
            <group android:id="@+id/first_group" >
                <item
                    android:id="@+id/coaching"
                    android:icon="@drawable/coaching"
                    android:title="Coaching"
                    android:checkable="true" />
                <item
                    android:id="@+id/training"
                    android:icon="@drawable/training"
                    android:title="Training"
                    android:checkable="true" />
            </group>
        </menu>
    </item>
    <group android:id="@+id/second_group" >
        <item
            android:id="@+id/new_registrations"
            android:icon="@drawable/new_registrations"
            android:title="New Registrations"
            android:checkable="true" />
        <item
            android:id="@+id/ready_certificates"
            android:icon="@drawable/ready_certificates"
            android:title="Certificates Ready To Collect"
            android:checkable="true" />
        <item
            android:id="@+id/allotted_certificates"
            android:icon="@drawable/allotted_certificates"
            android:title="Certificates Allotted So Far"
            android:checkable="true" />
    </group>
</menu>
  • Declare two MenuItem objects in the MainActivity.java and make them checked.
  • Add the following logic to the onNavigationItemSelected.

Your MainActivity.java should look like this:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    MenuItem prevItemOfFirstGroup;
    MenuItem prevItemOfSecondGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ... some code

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        prevItemOfFirstGroup=navigationView.getMenu().findItem(R.id.coaching);
        prevItemOfFirstGroup.setChecked(true);
        prevItemOfSecondGroup=navigationView.getMenu().findItem(R.id.new_registrations);
        prevItemOfSecondGroup.setChecked(true);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int groupId = item.getGroupId();
        if (groupId == R.id.first_group) {
            if (prevItemOfFirstGroup != null) {
                prevItemOfFirstGroup.setChecked(false);
            }
            prevItemOfFirstGroup = item;
        } else if (groupId == R.id.second_group) {
            if (prevItemOfSecondGroup != null) {
                prevItemOfSecondGroup.setChecked(false);
            }
            prevItemOfSecondGroup = item;
        }
        item.setChecked(true);

        // Handle navigation view item clicks here.
        int id = item.getItemId();
        // ... some code

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return false; // IMPORTANT! NOT TRUE!
    }
}