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.
Related
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.
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;
}
I am trying to enable/disable a refresh button when certain things happen in my app, but I get a null pointer exception that I can't figure out. I am setting a boolean addingRefresh or removingRefresh to true depending on the situation and then calling invalidateOptionsMenu() to enable or disable the button, however the menu item is returned null. I have searched the internet for why this may be but can't find anything.
Code for onCreateOptionsMenu() (called when invalidateOptionsMenu() is called)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (addingRefresh) {
//below line as well as other similar line cause exceptions
menu.findItem(R.id.action_refresh).setEnabled(true);
addingRefresh = false;
} else if (removingRefresh) {
menu.findItem(R.id.action_refresh).setEnabled(false);
removingRefresh = false;
} else if (addingLoading) {
}
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Thanks for any help!
Here is some cleaned up code for what you are trying to accomplish:
private MenuItem mMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
mMenuItem = menu.findItem(R.id.action_refresh)
return true;
}
private void setMenuItemEnabled(boolean enabled) {
mMenuItem.setEnabled(enabled);
}
Hope that helps!
Just for clearing the doubt.
You should use
menu.findItem(R.id.menuId);
after inflating the menu
for me cause of issue was wrong import:
android.widget.SearchView
instead
androidx.appcompat.widget.SearchView
and take care about namespaces
app:actionViewClass
I've met a serious problem. I want to add the MenuItem after the user successfully logged in.
At the beginning I use the following method to make it available.
The following code doesn't work now , I mentioned it as to illustrate my idea.
Private Menu mymenu;
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
mymenu=menu;
return super.onCreateOptionsMenu(menu);
}
public void process(){
MenuInflater inflater=this.getMenuInflater();
inflater.inflate(R.menu.my_menu,mymenu);
}
It seems this idea worked as I remembered.But after doing something I couldn't remember , It doesn't work!
After the debug , I find it's because mymenu=null .
I guessed there is a way to set mymenu=menu
Can anyone help?
The problem you could be having with that code is calling the super twice.
To do what you want, you could override onPrepareOptionMenu and set the visibility to hidden on all menu items you don't want until your state is such that you want to "add" it.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItem = menu.findItem(R.id.<id of menuitem>);
menuItem.setVisible(<condition>);
return true;
}
and just use standard code in onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.<id of menu with all items you'll need>, menu);
return true;
}
There is a method, onPrepareOptionsMenu(Menu menu) method which you could override and dynamically change the menus.
I have an options menu in an Activity with three MenuItems
create
select
delete
When there is no item currently selected I disable the MenuItem for delete.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.basic_menu, menu);
this.deleteCharMenu = menu.findItem(R.id.basic_menu_deletechar);
this.deleteCharMenu.setEnabled(this.current != null);
return true;
}
However onCreateOptionsMenu is not called every time the menu is opened (try and error got me this result).
So I stored the MenuItem in a class variable to be able to change its state whenever this.current changed its value.
Is there a more elegant method to ensure that the correct MenuItems are enabled and disabled? I am hoping for some kind of listener I can use?
overload the onPrepareMenu , it is called right before it's shown.