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

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;
}

Related

MenuItem text and icon are not being set

my application provides a list of recipes
at first all recipes are meat based and on the click of a switch in the setting page they become vegetable based
i want my app to change the titles and icons of the menu from meat based to vegetable based
i have put this code in the onPrepareOptionsMenu and it is being called but the titles are not being updated
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_drawer, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT > 11) {
invalidateOptionsMenu();
MenuItem item1 = menu.findItem(R.id.nav_all);
MenuItem item2 = menu.findItem(R.id.nav_beef);
MenuItem item4 = menu.findItem(R.id.nav_chicken);
Log.d("ISVEG", MeApplication.getIsVeg().toString());
if (MeApplication.getIsVeg()) {
item1.setTitle("Omlets");
item1.setIcon(ContextCompat.getDrawable(this, R.drawable.eggs));
item2.setTitle("Broccoli");
item2.setIcon(ContextCompat.getDrawable(this, R.drawable.broccoli));
item4.setTitle("Tomato");
item4.setIcon(ContextCompat.getDrawable(this, R.drawable.tomato));
} else {
item1.setTitle("All meat");
item1.setIcon(ContextCompat.getDrawable(this, R.drawable.allmeat));
item2.setTitle("Beef");
item2.setIcon(ContextCompat.getDrawable(this, R.drawable.steak));
item4.setTitle("Chicken");
item4.setIcon(ContextCompat.getDrawable(this, R.drawable.thanksgiving));
}
}
return super.onPrepareOptionsMenu(menu);
}
Remove invalidateOptionsMenu(); from onPrepareOptionsMenu. Its not required since onPrepareOptionsMenu will prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown

How to replace menu dynamically with another in 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

Android: Can't toggle MenuItem icon

I'm tring to change a menuitem icon, but the icon is not being changed.
Here is how I find the MenuItem (works fine):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_location_actions, menu);
mActionLocation = menu.findItem(R.id.action_location);
return super.onCreateOptionsMenu(menu);
}
Method UpdateIcon: I took a screenshot so you can see (in the red frame) that the images have been found by the system.
The mActionLocation is a MenuItem which is initialized before this Method is called and is not null.
Anyone has an idea?
UPDATE (solution by help from #vinitius)
The UpdateIcon method:
private void UpdateIcon(boolean locationOn) {
mActionLocation = locationOn; // mActionLocation is a global boolean
invalidateOptionsMenu();
}
The onPrepareOptionsMenu override:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Toggle location icon
if (mActionLocation) {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_on_white_24dp);
} else {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_off_white_24dp);
}
return super.onPrepareOptionsMenu(menu);
}
You need to use onPrepareOptionsMenu(Menu menu):
This is called right before the menu is shown, every time it is shown.You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
Inside this method, retrieve your item as you do in onCreateOtionsMenu and check whether your gps in os or not to modify your item icon.

android: different menu depending on situation

hello i would like to present a different menu in my activity depending on some condition. i tried like this.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
if (specialMode)
{
inflater.inflate(R.menu.menuA, menu);
}
else
{
inflater.inflate(R.menu.menuB, menu);
}
return true;
}
however, i always get the same menu, no matter what the value of specialMode is.
onCreateOptionsMenu is called only once ( upon first creation), try using onPrepareOptionsMenu method it get called every time menu is shown.

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