I want replace current menu of action bar with another when change action bar.
To achieve this I wrote:
getMenuInflater().inflate(R.menu.menu2, menu_bar);
But it add new menu at the rest of current menu.
What i want is remove current and add the new one.
How can I do that?
You can call invalidateOptionsMenu(), which will cause the system to call onPrepareOptionsMenu(Menu menu) again. Inside this method, just check which one of your two menus you should inflate, after calling menu.clear().
Note that invalidateOptionsMenu() has been introduced in API 11 (Honeycomb). If you need to target a lower API level, you can store your Menu object from onPrepareOptionsMenu(Menu menu) as an attribute in your Activity, and directly call clear() on it, then inflate the needed menu as you are already doing.
If you're doing it when changing fragments then:
(1) Create a menu file for fragment
(2) onCreate() method of Fragment set setHasOptionsMenu(true);
(3) Override onCreateOptionsMenu, where you'll inflate the fragment's menu and attach it to your standard menu.
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu2, menu);
}
(4) Override onOptionItemSelected in your fragment for item handlers.
Check the link below for more details..
the answer below is correct just has a extra point that will said
You can call invalidateOptionsMenu(), which will cause the system to call onPrepareOptionsMenu(Menu menu) again. Inside this method, just check which one of your two menus you should inflate, after calling menu.clear().
Note that invalidateOptionsMenu() has been introduced in API 11 (Honeycomb). If you need to target a lower API level, you can store your Menu object from onPrepareOptionsMenu(Menu menu) as an attribute in your Activity, and directly call clear() on it, then inflate the needed menu as you are already doing.
point : at onPrepareOptionsMenu(Menu menu) should not call older menu item that lead to Error
public boolean onPrepareOptionsMenu(Menu menu) {
// Error -> menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
menu.clear();
getMenuInflater().inflate(current_menu, menu);
return super.onPrepareOptionsMenu(menu);
}
Related
In my app I have some activities without menu items, that use the following override:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.appbar_menu_empty, menu);
return true;
}
This works good. If I remove the override, I get the same effect on Android 5.1, i.e. an action bar with no icons.
So the question is: can I drop the override?
The documentation of Activity.onCreateOptionsMenu states:
The default implementation populates the menu with standard system menu items.
What does that mean? Do I need to expect that Android comes up with some buttons I did not explicitly add?
You can remove OncreateOptionsMenu() if you dont want to have menu items.
If you want to add menu items, edit the menu.xml file in resources/menu directory.
From the docs the method is defined in Activity class as below
Initialize the contents of the Activity's standard options menu. You
should place your menu items in to menu.
This is only called once, the first time the options menu is
displayed. To update the menu every time it is displayed, see
onPrepareOptionsMenu(android.view.Menu).
The default implementation populates the menu with standard system
menu items. These are placed in the android.view.Menu.CATEGORY_SYSTEM
group so that they will be correctly ordered with application-defined
menu items. Deriving classes should always call through to the base
implementation.
You can safely hold on to menu (and any items created from it), making
modifications to it as desired, until the next time
onCreateOptionsMenu() is called.
When you add items to the menu, you can implement the Activity's
onOptionsItemSelected(android.view.MenuItem) method to handle them
there.
Parameters:
menu The options menu in which you place your items. Returns:
You must return true for the menu to be displayed; if you return false it will not be shown.
public boolean onCreateOptionsMenu(Menu menu) {
if (mParent != null) {
return mParent.onCreateOptionsMenu(menu);
}
return true;
}
also check this SO thread out onCreateOptionsMenu() calling super
check the code for Activity class here http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/app/Activity.java#Activity.onCreateOptionsMenu%28android.view.Menu%29
see some sample code here where you need to show option in action bar menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_act_add_recipe, menu);
super.onCreateOptionsMenu(menu, inflater);
}
/res/menu/menu_act_add_recipe.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_add_image"
android:icon="#drawable/ic_tab_add_image_white"
android:orderInCategory="100"
android:title="#string/action_preview"
app:showAsAction="always" />
<item
android:id="#+id/action_recipe_preview"
android:icon="#drawable/ic_tab_check_white"
android:orderInCategory="100"
android:title="#string/action_preview"
app:showAsAction="always" />
I get the menu item and then i try to set the visibility but the menu item is always shown. Can anyone see where I am making a mistake?
The menu item is not null and thus is allocated so thats not it.
MenuItem done = menu.findItem(R.id.action_done);
//animate the list view
if (isListEditing) {
done.setVisible(true);
menuItem.setTitle(this.getString(R.string.EditKey));
isListEditing = false;
adapter.endEdit();
} else {
done.setVisible(false);
menuItem.setTitle(this.getString(R.string.DoneKey));
isListEditing = true;
adapter.makeEditable();
}
this.invalidateOptionsMenu();
I get the menu reference here:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_test_results, menu);
this.menu = menu;
return true;
}
Update:
I was under the impression that you had to invalidate the options menu after you made an edit. But that is what was cause the edits to not go through.
According to the docs, what invalidateOptionsMenu() does is:
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.
That means onCreateOptionsMenu will get called again, inflating your original menu layout, and thus discarding your previous changes to the menu items visibility.
The recommended approach to modify the menu content dynamically is to use onPrepareOptionsMenu. So whenever you need to update menu items, you can call invalidateOptionsMenu(), and then inside onPrepareOptionsMenu, you set the menu items visibility.
There is no need for
this.invalidateOptionsMenu();
Take that off and it should work fine.
This works just fine but If I'm on a different activity and I use the back button, it won't update the action bar because the activity is already created and it won't update the action bar. Already tried to use supportInvalidateOptionsMenu() on the on_create method but it didn't work.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
Cursor cursor = messages.getMessages();
if(cursor.getCount()>0){
inflater.inflate(R.menu.actionbar1, menu);
}else{
inflater.inflate(R.menu.actionbar2, menu);
}
return super.onCreateOptionsMenu(menu);
}
As the documentation for onCreateOptionsMenu(Menu) states:
This is only called once, the first time the options menu is
displayed. To update the menu every time it is displayed, see
onPrepareOptionsMenu(Menu).
So going back to an already created Activity does not trigger onCreateOptionsMenu(Menu) again.
What I suggest you to do is create just one menu containing all the menu items and selectively activate/deactivate them in onPrepareOptionsMenu(Menu) based on one or more flags. Then put invalidateOptionsMenu() in onResume() which is called every time the Activity is shown.
Hope it helps
Try calling invalidateOptionsMenu whenever you need to change the icon.
It will destroy your menus and re-inflate them by calling onPrepareOptionsMenu.
I am a noob to android and i am trying to inflate two different menus depending on user selection. However, the menu isn't switching. The same menu inflates everytime regardless of what the user selects. I've tried and checked my if/else statmente with various parameters and the menu inflater still doesn't respond properly by only inflating the same menu. Any help is greatly appreciated.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
popUpMenu = getMenuInflater();
//popUpMenu.inflate(R.menu.cool_menu, menu);
if(mypodcast==null){
popUpMenu.inflate(R.menu.cool_menu, menu);
}else {
popUpMenu.inflate(R.menu.podcast, menu);
}
return true;
}
The same menu inflates everytime regardless of what the user selects.
That is because onCreateOptionsMenu() is not called each time you open the menu, from the documentation:
This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu)
If you want to change menus you need to do this in onPrepareOptionsMenu(). However I don't believe that you can or should inflate a new menu every time onPrepareOptionsMenu() is called. But you can combine the two menus and change the visibility of each menu item according to what you want in this method.
I read that it was possible to display a different menu for each tab on this guide.
I have 3 tabs initialized in MainActivity.
Even if I call onPrepareOptionsMenu() or onCreateOptionsMenu() in each included Activity, they are never executed.
I just succeed in displaying a menu on MainActivity's initialization...
MainActivity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu_tab_1, menu);
return true;
}
This menu is shown.
One of my TabActivities:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu_tab_2, menu);
return true;
}
This menu is not refreshed when I change the tab.
But I tried multiple combinations (onCreate / onPrepare, Override...) without success.
How do it properly?
Thanks
You have two options to do this.
First, you can override the onKeyDown method, and detect when the user presses the menu, and instead of letting the main activity handle it, dispatch the event to the active tab activity.
Else, you can use Fragments instead of activities inside your Tab host. The fragment mechanism is great to combine the Options Menus from multiple source (usually one activity and one or more fragments).