8

I have NavigationView with multiple Groups which is based on some condition I need to hide & show the group. How I can achieve this.

My Sample NavigationView menu

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

 <group android:checkableBehavior="single"
        android:id="@+id/grp_employee"
        >

        <item
            android:id="@+id/help"
            android:checked="false"
            android:icon="@drawable/ic_help"
            android:title="@string/app_name" />

.......
</group>

<group android:checkableBehavior="single"
        android:id="@+id/grp_admin"
        >

        <item
            android:id="@+id/admin_help"
            android:checked="false"
            android:icon="@drawable/ic_help"
            android:title="@string/app_name" />

.......
</group>

<group> ... </group>

</menu>

This is the my NavigationView file. I just want to show only one group view at a time hide all others group.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
  • 3
    why negative vote explain whats wrong. If you guys unable to answer why put a negative vote – Maheshwar Ligade May 13 '16 at 04:35
  • For those who get here now, I would like to add that you can also set the group's visibility in the xml ( android:visible="false ). That makes it the default when you launch the application. Then you can alter it programmatically when you have to – Dan Jan 11 '21 at 19:14

2 Answers2

13

Well, you can use Menu.setGroupVisible (int group, boolean visible) to hide or show menu group.

navigationView.getMenu().setGroupVisible(R.id.group_id,false);//to hide
navigationView.getMenu().setGroupVisible(R.id.group_id,true);//to show
halfer
  • 19,824
  • 17
  • 99
  • 186
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • hello ,but programatically i need to check is that group_id is visible or not ,so how can i? is thare any idea – sunita Dec 09 '17 at 07:10
  • @sunita not sure, but try this method https://developer.android.com/reference/android/view/Menu.html#hasVisibleItems() – Bharatesh Dec 09 '17 at 08:11
  • I have tried several methods, but this one works the most and simple – Aldan Jul 02 '20 at 06:57
0

You can check visibility for one of the items in group, and the result is equal to group visibility. For example, check admin_help visibility by this line:

navigationView.getMenu().findItem(R.id.admin_help).isVisible()

and the results is same to check grp_admin visibility

amir_a14
  • 1,478
  • 10
  • 15