Home » Android » android – How to set visibility for an actionbar menu group?

android – How to set visibility for an actionbar menu group?

Posted by: admin May 14, 2020 Leave a comment

Questions:

UPDATE

Originally I was using ActionBarSherlock I have since created a brand new project using a native android action bar just to test this and I am still getting the same problem.

I am successfully showing/hiding items but not groups. I am rapidly coming to the conclusion that there is a bug in the ActionBar and it is not possible to programmatically set the visibility of a group

END of UPDATE

Given the following menu When accessing the Group I get a null pointer exception

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

    <item android:id="@+id/action_settings"
        android:title="@string/settings"
        android:orderInCategory="100"
        android:showAsAction="never"/>

        <group android:id="@+id/mnu_text_group"
           android:visible="false">
            <item android:id="@+id/mnu_text_type"
                android:enabled="true" 
                android:visible="true"
                android:icon="@drawable/ic_action_text_icon"
            android:showAsAction="always"> 
            </item>
            <item android:id="@+id/text_color"
                android:enabled="true" 
                android:visible="true" 
                android:showAsAction="always" 
                android:icon="@drawable/ic_action_color_line">
            </item>
        </group>        
    <item android:id="@+id/mnu_images"
...

In the onPrepareOptionsMenu of the relevant activity I have

public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
    mnuImage.setEnabled(mEnableImageMenu);
    mnuTextGroup.setVisible(false);
    ...

The call to mnuTextGroup.setVisible(false); raises a null pointer exception

However, by changing the find method to find an item within the group works fine e.g. MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_type);but obviously this works just for the specific item. I know groups are designed for exactly this purpose, to be able to set the visibility of and enable/disable all items within the group but I have been unable to find a way to do this programatically.

How to&Answers:

Finally found the solution
I needed to use the setGroupVisible() method of the menu object passed into the onPrepareOptionsMenu() method

This is what worked for me

Instead of

MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuImage.setEnabled(mEnableImageMenu);
mnuTextGroup.setVisible(false);

This is what I needed

menu.setGroupVisible(R.id.mnu_text_group, false);

Answer:

Simple and one line

navigationView.getMenu().setGroupVisible(R.id.groupstaff, false);

Answer:

Use In Activity Where You Want to Hide

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getSupportMenuInflater().inflate(R.menu.main_itemlist, menu);
        boolean isdown = false;

        menu.findItem(R.id.addwork).setVisible(isdown);

        MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
            mnuTextGroup.setVisible(isdown);

        return true;
    }

Answer:

altering the options menu needs to be done within onPrepareOptionsMenu or else it sometimes doesn’t work (not sure exactly why, hopefully someone else can elaborate):

@Override public boolean onPrepareOptionsMenu(Menu menu) {
  super.onPrepareOptionsMenu(menu);
  // set visibility of menu items here
  return true;
}