0

I create a menu in an activity with the following code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu1, menu);

return true;

}

But so, I can open and close the menu any time. I want that the menu can be opened if a boolean is true, if the boolean is false, the user should not be able to open the menu...

Hariharan
  • 24,741
  • 6
  • 50
  • 54
Jan Knoblauch
  • 219
  • 1
  • 3
  • 20
  • http://stackoverflow.com/questions/5440601/android-how-to-enable-disable-option-menu-item-on-button-click – Hardik Nov 04 '13 at 11:20

2 Answers2

1

just put a if statement in your code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
if(yourbooleanvariable)
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}

If you want to preserve the value of boolean vairable use sharedpreferences here is the link How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
Shaji Thorn Blue
  • 589
  • 2
  • 9
  • 19
0

I guess you can just add an ifstatement to the function:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    bool x = true;
    if(x){
       getMenuInflater().inflate(R.menu.menu1, menu);
       return true;
    } else {
      return false;
    }
}
Baklap4
  • 3,914
  • 2
  • 29
  • 56