21

I have a basic problem where an initially hidden MenuItem is unable to be toggled to visible. As a caveat, I am using ActionBarSherlock, but I wanted to see if anyone knew if this was a known issue of Android or I am doing something terrible before investigating whether this is an issue inside of ABS. Code as follows:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu_xml, menu);

    mMenuItem = menu.findItem(R.id.menu_item);
    mMenuItem.setVisible(false);

    return true;
}

// Somewhere elsewhere
// MenuItem is never visible after this line is executed
mMenuItem.setVisible(true);

I have also tried to move the mMenuItem assignment and visibility into a call to onPrepareOptionsMenu but the same behavior is shown.

Thanks!

9
  • Is it just the menu_item that isn't displaying, or is the entire menu_xml not displaying? Because you are inflating the menu_xml, but I don't see where you're using it or adding it to anything.
    – Scen
    Commented Jul 10, 2012 at 21:46
  • You don't need to attach it to anything, do you? The MenuInflater takes care of that? Commented Jul 10, 2012 at 21:59
  • I am using ABS and I was able to show or hide the menu with no problems. Although my menuitem is added by code, instead of inflated by xml. Try the old fashion way of adding menu: menu.add(...).setIcon().setShowAsAction();
    – azgolfer
    Commented Jul 10, 2012 at 22:01
  • I tried what you suggested, and it does not work azgolfer :( Commented Jul 10, 2012 at 22:12
  • If you remove the code setVisible(false) inside onCreateOptionsMenu(Menu), would the menu shows up? If so, then try hiding it in onPrepareOptionsMenu() to see if it goes away.
    – azgolfer
    Commented Jul 10, 2012 at 23:08

4 Answers 4

26

The problem is you are not telling Android that it needs to update the menu. This drove me nuts for the last hour until I figured out a solution. I don't think it's as apparent on pre-HC because menu items aren't always visible on the screen like they are in HC+.

On your activity, simply call:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
this.invalidateOptionsMenu();
}

That will trigger a call to the onCreateOptionsMenu() event again, so if you're setting the visibility in that function for initialization you'll need to take into account then if you want the option to show or not.

5
  • 8
    *supportInvalidateOptionsMenu() (He is using ABS) Commented Jul 31, 2012 at 15:53
  • Never seen ABS so couldn't tell you specifics there.
    – Tony Maro
    Commented Jul 31, 2012 at 18:47
  • And how do you do this for pre-Honeycomb devices?? Commented Feb 11, 2013 at 18:25
  • for me this exact code worked in a 4.4 device, using ABS
    – voghDev
    Commented Nov 6, 2014 at 9:08
  • 3
    Be careful if the menu xml sets visibility to false. I was setting the visibility before invalidating the menu, which reinflated the xml thus ignored my setVisibility value
    – Maragues
    Commented May 29, 2015 at 10:04
6

I had the same problem and I found out that setVisible(true) works when there is at least another MenuItem visible. I hope this can be helpful to someone.

1
  • In my case, there have to be at least 3 items visible in order for setVisible or setEnabled to work.
    – lenooh
    Commented Sep 25, 2015 at 21:00
6

I understand this is old question.
But I've solved it by placing inside onPrepareOptionMenu() {...}

private boolean mShowVisible=false;

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

     menu.findItem(R.id.menu_item).setVisible(mShowVisible);

     return super.onPrepareOptionsMenu(menu);
}

whenever you want to set visible or not just call it as:

mShowVisible = true; // or false
invalidateOptionMenu();
0
4

I found that using a view's post(Runnable) method to setVisible does the trick, so something like...

view.post(new Runnable() {

                @Override
                public void run() {
                    menu.findItem(id).setVisible(true);
                }

            });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.