Android - inclusion of menu items by code

I need to enable MenuItem when the previous screen (Activity) returns.

I tried this code:

... ((MenuItem) findViewById (R.id.menu_how)) setEnabled (true). ...

but a null pointer exception is thrown.

By the way, the menu_how parameter is set to false in xml; and the code is part of the onActivityResult call (int requestCode, int resultCode, Intent).

+4
source share
4 answers

I found something on the android dev site that might be useful (see the section "Changing menu items at runtime")

He said that the onCreateOptionsMenu() method only starts when a menu for an action is created, and that happens when that action starts. Therefore, if you want to change menu items after a menu / action has been created, you should instead override the onPrepareOptionsMenu() method. Find the link for full details.

EDIT:

Just did it and it works great. I use one boolean var per menuItem , which represents whether this element should be included or not. This is my code:

 /*************************************Game Menu**************************************/ @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.gm_new_game: //newGame(); return true; case R.id.gm_stand_up: //some code when "gm_stand_up" button clicked.. return true; case R.id.gm_forfeit: //some code when "gm_forfeit" button clicked.. return true; case R.id.gm_surrender: //some code when "gm_surrender" button clicked.. return true; case R.id.gm_exit_table: exitTableCommand(); return true; default: return super.onOptionsItemSelected(item); } } @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.findItem(R.id.gm_forfeit).setEnabled(forfeitMenuButtonIsEnabled); menu.findItem(R.id.gm_surrender).setEnabled(surrenderMenuButtonIsEnabled); menu.findItem(R.id.gm_new_game).setEnabled(newGameMenuButtonIsEnabled); menu.findItem(R.id.gm_stand_up).setEnabled(standUpMenuButtonIsEnabled); return super.onPrepareOptionsMenu(menu); } 
+6
source

Try using menu.findItem(R.id.menu_how) in onCreateOptionsMenu and save the link for later use.

This should work fine with enabled , however, I found that setting a menu item to invisible in XML means that you cannot show / hide it programmatically.

+6
source

where do you call it? (Sorry, did not read carefully). It seems to me that you need to call it after the menu is too high (usually in OnCreateOptionsMenu). To do this, you can set the variable to true when another activity returns, and then ((MenuItem)findViewById(R.id.menu_how)).setEnabled(mMyBooleanField) in OnCreateOptionsMenu after calling inflater.inflate.

Edit: for this in code, it might look something like this:

At the top of the class (along with all other members of the class):
Boolean mEnableMenuItem = false;

In OnCreateOptionsMenu:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_main, menu); ((MenuItem)findViewById(R.id.menu_how)).setEnabled(mEnableMenuItem );

In OnActivityResult:
mEnableMenuItem = true;

+1
source

Keep a link to the menu in your activity:

 private Menu mMenu; 

Then:

 public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_note, menu); mMenu = menu; return true; } 

Now, to access the menu items anywhere in your activity, use the same code:

 mMenu.findItem(R.id.menu_how).setVisible(false); 

or

 mMenu.findItem(R.id.menu_how).setEnabled(true); 
0
source

All Articles