0

I'm trying to add dividers between items of the same group in NavigationView, I know this is possible using different groups but I need them to be in the same group with single checkablebehavior

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group
    android:id="@+id/first"
    android:checkableBehavior="single">
    <item
        android:id="@+id/all_categories"
        android:icon="@mipmap/ic_launcher"
        android:title="All Categories" />
    <item android:title="Standard Categories" />
    <item
        android:id="@+id/transit"
        android:icon="@drawable/ic_airplane_1_2"
        android:title="Transit" />
    <item
        android:id="@+id/memberships"
        android:icon="@drawable/ic_id_card_3_2"
        android:title="Memberships" />
</group>
</menu>
atabouraya
  • 3,233
  • 1
  • 26
  • 31

1 Answers1

0

You can actually still do it by using different groups but then what you will do is to de-select the previously selected item in another group. You can do it as follows (taken from the second popular answer in How to create a simple divider in the new NavigationView? - this works and I have checked it - it's quite intuitive):

public boolean onNavigationItemSelected(final MenuItem menuItem) {

    //if an item from extras group is clicked,refresh NAV_ITEMS_MAIN to remove previously checked item
    if (menuItem.getGroupId() == NAV_ITEMS_EXTRA) {


        navigationView.getMenu().setGroupCheckable(NAV_ITEMS_MAIN, false, true);
        navigationView.getMenu().setGroupCheckable(NAV_ITEMS_EXTRA, true, true);
       }else{

        navigationView.getMenu().setGroupCheckable(NAV_ITEMS_MAIN, true, true);
        navigationView.getMenu().setGroupCheckable(NAV_ITEMS_EXTRA, false, true);


    }
    //Update highlighted item in the navigation menu
    menuItem.setChecked(true);
}
Community
  • 1
  • 1
ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • I am trying to avoid that solution, I know this will work but searching for a simple one – atabouraya Jan 10 '16 at 09:29
  • Unfortunately at this time there doesn't seem to be a way to do it. The NavigationView is a brand new addition - for now it supports only one clickable element in a group and only groups are divided by the divider you wanted. Please keep in mind that the NavigationView is intended to be tied to a menu resource file - maybe the reason why Google did not support individual sub menu lists – ucsunil Jan 10 '16 at 09:34