7

I have a custom menu options that I want to disable it from popping up if a button on screen is clicked..

I thought of using this code but it doesnt work:

@Override
 public boolean onPrepareOptionsMenu (Menu menu) {
     if (Schedule)
         menu.getItem(1).setVisible(View.GONE);
     return true;
 }

Is there a way to prevent the menu button from doing anything? Thanks.

Omar
  • 7,835
  • 14
  • 62
  • 108

1 Answers1

15

According to the documentation:

You must return true for the menu to be displayed; if you return false it will not be shown.

So I'm guessing this will work:

@Override
 public boolean onPrepareOptionsMenu (Menu menu) {
     .... Code .....
     return !Schedule;
 }

That is assuming that you want the menu to display when Schedule is equal to false.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • 2
    In other words, just return `false` to disable the menu. Worked for me. – Stephen Hosking Nov 27 '12 at 04:49
  • Yeah, but I was addressing his question specifically at the time. Usually there's a condition to disable it anyway as you would simply not create one to begin with if you want it always disabled. – DeeV Nov 27 '12 at 05:22
  • This didn't immediately work for me, but this solved my problem: http://stackoverflow.com/a/9546316/602245 – Brett Feb 27 '13 at 17:45