How to replace menu dynamically with another in Android - android

I have a ListActivity (SherlockListActivity) and its content can be dynamically changed by the user. When the content changes, the options menu should be replaced.
As I learned, instead of onCreateOptionsMenu I should use onPrepareOptionsMenu that is (supposed to be) called every time the user selects the menu.
This is called right before the menu is shown, every time it is shown.
I have the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.clear();
if (UserOption == UserOption1)
getSupportMenuInflater().inflate(R.menu.menu_option1, menu);
else
getSupportMenuInflater().inflate(R.menu.menu_option2, menu);
return super.onPrepareOptionsMenu(menu);
}
It is called only once during debug, so I always have the same menu.
What should I set to make it work?

Create and prepare the options menu for changing and its item selection method
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if(menuString=="red"){
inflater.inflate(R.menu.red_menu, menu);
}else if(menuString=="green"){
inflater.inflate(R.menu.green_menu, menu);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
return true;
case R.id.help:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Like whenever you want to change menu call
String menuString="red or green";
invalidateOptionsMenu();
Like others told, if you want to have static menu use onCreateOptionsMenu, and if you want to change its visibility dynamically use onPrepareOptionsMenu along with onCreateOptionsMenu

Related

how can i change the visibility of a menu item inside onOptionsItemSelected(..)

i have three menu items "enable, diable, exit". what i want to do is, when the R.id.menu_enable_bt is chosen, i want to disable it using
menu.findItem(R.id.menu_enable_bt).setVisible(false);
but i can not call
menu.findItem(R.id.menu_enable_bt).setVisible(false);
from inside the onOptionsItemSelected(..) method.
how can i change the visibility of a menu item inside onOptionsItemSelected(..)
CODE:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
Log.w(TAG, SubTag.msg("onCreateOptionsMenu"));
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
if (this.mBTAdapter.isEnabled()) {
menu.findItem(R.id.menu_enable_bt).setVisible(false);
menu.findItem(R.id.menu_disable_bt).setVisible(true);
} else {
menu.findItem(R.id.menu_enable_bt).setVisible(true);
menu.findItem(R.id.menu_disable_bt).setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
Log.w(TAG, SubTag.msg("onOptionsItemSelected"));
switch (item.getItemId()) {
case R.id.menu_enable_bt:
Log.d(TAG, SubTag.bullet("onOptionsItemSelected", "menu_enable_bt"));
//menu.findItem(R.id.menu_enable_bt).setVisible(false); **how to do this**
this.mATEnableBT = new ATEnableBT();
this.mATEnableBT.execute();
break;
case R.id.menu_disable_bt:
Log.d(TAG, SubTag.bullet("onOptionsItemSelected", "menu_disable_bt"));
break;
case R.id.menu_exit:
Log.d(TAG,SubTag.bullet("onOptionsItemSelected", "menu_exit"));
finish();
break;
}
return super.onOptionsItemSelected(item);
}
you can use invalidateOptionsMenu() which will forse onCreateOptionsMenu to be called again.
From the documentation
Declare that the options menu has changed, so should be recreated. The
onCreateOptionsMenu(Menu) method will be called the next time it needs
to be displayed.
There you can check your conditions and undertake the needed actions
See the javadoc for onPrepareOptionsMenu. This is your opportunity to make changes to a menu just before it becomes visible to the user. So you should maintain some member variables describing what should be visible and use that to modify the menu items here.

Variable to keep track of contentView, then call inalidateOptionsMenu()

My main goal is to refresh the options menu depending on what layout is current in the Activity. I don't want the buttons on my Action Bar when R.layout.preweb is active, but when setContentView is changed to R.layout.main becomes active, invalidateOptionsMenu and inflate. Here's what I have now:
private int mViewMode;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return mViewMode == 2;
}
And throughout my activity, in random methods, I have this:
setContentView(R.layout.preweb);
mViewMode = 1;
//or somewhere else
setContentView(R.layout.main);
mViewMode = 2;
invalidateOptionsMenu();
So basically, whenever mViewMode is set to 2, invalidateOptionsMenu is called and then onCreateOptionsMenu should retrigger, and being mViewMode now equals 2, I should then inflate. But nothing is happening - it never inflates.
What's up here? Any suggestions?
On invalidating menu using invalidateOptionsMenu(), method onPrepareOptionsMenu(android.view.Menu) will be called, not onCreateOptionsMenu(). So you need to override this method, and you need to write code to show menu item or hide menu item.
For example
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (mViewMode == 2) {
// Need to show menu items
menu.findItem(R.id.menuitem1).setVisible(true);
menu.findItem(R.id.menuitem2).setVisible(true);
} else {
// Need to hide menu items
menu.findItem(R.id.menuitem1).setVisible(false);
menu.findItem(R.id.menuitem2).setVisible(false);
}
return true;
}
and onCreateOptionsMenu() does not gets called when you say invalidateOptionsMenu(), so no need to return false from there. You always need to return true from it.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}

option menu not clicked at first time in sherlockfragment

I have used option menu in my fragment.The problem is that when i go first time to fragment ,the option menu click event is not called.But when i go to another fragment.and again revist that fragment then the option menu click event is called...
following is the code
//Creating the option menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.newcarmenu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
break;
}
return super.onOptionsItemSelected(item);
}
please tell me why this happen?
Right now, you are returning super.onOptionsItemSelected(item) every time, so the selection is being passed on. You need to return true when your MenuItem has been selected. Try this instead:
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
return true;
default:
return super.onOptionsItemSelected(item);
}

Android access menu from onOptionsItemSelected

I have the following in my activity (sorry new to Java/Android):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.selectItem:
// menu.add(...) --> how to get the menu instance?
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am wondering, how can I access a menu object in onOptionsItemSelected? For example, how would I go about adding a new view to the options menu based on the selection of an existing menu item? Is the answer related to "onPrepareOptionsMenu"?
you should use SubMenu for such things ... remeber that you cant add submenu to another submenu ... so only Menu->Submenu is possible you can't do stuff like this Menu->Submenu->Submenu (while Submenu is Dialog with choices)

Android - Enabling MenuItems by code

I need to enable a MenuItem when a previous screen (Activity) returns.
I tried this code:
...
((MenuItem)findViewById(R.id.menu_how)).setEnabled(true);
...
but a null pointer exception is launched.
BTW, the menu_how is set to false in xml; and the code is part of onActivityResult(int requestCode, int resultCode, Intent data) call.
Try using menu.findItem(R.id.menu_how) in onCreateOptionsMenu and save a reference for later use.
This should work fine with enabled, however, I've found that setting a menu item to invisible in the XML means you can't show/hide it programmatically.
I found something at the android dev site that might be helpful (look for the section "Changing menu items at runtime")
It said that the onCreateOptionsMenu() method fired only when the the menu for the activity is created, and it happens when this activity starts. So if you want to change the menu items after the menu/activity was created, you need to override the onPrepareOptionsMenu() method instead. search the link for full details.
EDIT:
Just made it and it's working fine. I'm using one boolean var per menuItem which represents if this item should be enabled 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);
}
where are you calling this? (Sorry, didn't read carefully) I think you need to call it after the menu is inflated (usually in OnCreateOptionsMenu). To do this, you can set a variable to true when the other Activity returns, then do ((MenuItem)findViewById(R.id.menu_how)).setEnabled(mMyBooleanField) in OnCreateOptionsMenu after the call to inflater.inflate.
Edit: To accomplish this in code, it might look something like this:
At the top of the class (along with all the other class members):
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;
Keep a reference to 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 menu items anywhere in your activity use similar code to this:
mMenu.findItem(R.id.menu_how).setVisible(false);
or
mMenu.findItem(R.id.menu_how).setEnabled(true);

Categories

Resources