Android: Multiple Option Menus in one Activity - android

I have an Activity containing a ViewFlipper and would like to show a different option menu for each view in that ViewFlipper. That is, the type of menu displayed when the menu button is pressed would depend on the type of the current view.
However, onCreateOptionsMenu() is called only once (when showing the option menu for the first time), so creating the different menus can't be implemented there.
How could I solve this?

First read about onPrepareOptionsMenu(Menu menu)
Each time the user presses the Menu on their Android device while inside one of your activities, the onPrepareOptionsMenu method is called. The first time the menu is shown (i.e. only once), the onCreateOptionsMenu method is called.
Basically, the onPrepareOptionsMenu method is where you should make any changes such as enabling/disabling certain menu items, or changing the menu item text depending on the circumstances.
So do this (Don't use onCreateOptionsMenu(Menu menu) )
//Dynamically create context Menu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getMenuInflater();
if(condition_true)
inflater.inflate(R.menu.menu_one, menu);
else
inflater.inflate(R.menu.menu_two, menu);
return super.onPrepareOptionsMenu(menu);
}

Related

How to dynamically enable/disable entire overflow menu?

I would like to enable/disable the entire overflow menu in my Toolbar at runtime according to certain user actions. This is all within the same fragment. I tried just returning out of onPrepareOptionsMenu() if the condition holds, which hides the three dot icon as soon as the user touches it. However, I can't figure out how to then show the options menu again.
Calling getActivity().invalidateOptionsMenu() invokes onPrepareOptionsMenu() again, where you can load/not load the menu based on your conditions.
Just want to add one more thing if you want to show or hide the menu again without even inflating them. You can use oncreateoptions menu parameter like this
Menu overflow;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
this.overflow = menu;
return super.onCreateOptionsMenu(menu);
}
and then make a function to show or hide any menu item according to the index. for e.g.
public void hideorShowMenuItems(boolean bool){
overflow.getItem(1).setVisible(bool);
overflow.getItem(2).setVisible(bool);
overflow.getItem(3).setVisible(bool);
}

Android: hide action bar menu views regardless the menu items

I am using navigation drawer to switch between fragments.
And my action bar menu items are different regarding to the fragments.
I want to hide the menu item views when drawer is opened.
Referring to the doc, it suggest to find the menu item by ID and hide the item.
However, my menu items are different with fragments, so, how can I simply hide/show the menu item views on the action bar? Is there some flag to control it?
By the way, I see in the DOC it says I can return false in onPrepareOptionsMenu() to make it not displayed, however I tried and ended up in vain. Did I misunderstand it?
public boolean onPrepareOptionsMenu (Menu menu)
Added in API level 1
Prepare the Screen's standard options menu to be displayed. 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.
The default implementation updates the system menu items based on the activity's state. Deriving classes should always call through to the base class implementation.
Parameters
menu The options menu as last shown or first initialized by onCreateOptionsMenu().
Returns
You must return true for the menu to be displayed; if you return false it will not be shown.
If people are still visiting the link, this is how we disable the options menu or options menu items for a fragment.
1) In the onCreateView() method of fragment add the following code
setHasOptionsMenu(true);// then only we can work with the menu items.Without this onPrepareOptionsMenu() method is not called
.
Now override the onPreparedOptionsMenu() method and hide the menu items. If you hide all the menu items the menu will not at all be shown. or use menu.clear() to clear all the menu items
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_contacts).setVisible(false);
--------------
}

Fragment Specific Actionbar MenuItems

I'm building an application with ActionBarSherlock that uses the Dropdown list navigation style. I have it set that each dropdown list item loads a different fragment, and that works fine. What doesn't work is the menu items in the actionbar. I have setHasOptionsMenu(true) in the fragments that I want to have menu items, as well as
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
for the menus in the fragments. Every time I change fragments, I don't want the menu items appended which is what's happening. When one fragment is selected, the menu loads fine, then a different fragment is selected that isn't supposed to have menu items, and the menu items are the same as the previous fragment. Then if I go back to the first fragment, the menu items get doubled because they keep getting appended. How can I control this?
In the normal case the menu shouldn't be appended. What does your menu.xml look like? Do you have id's set? Maybe creating a menu in the Activity?
I figured it out. I wasn't using FragmentTransaction to load the fragments.

Handling Action Bars with two fragments

I have a layout with two fragments and both fragments have their own action bars, each of which have their own action items and menus. When my app is in landscape mode and both fragments are displayed on the screen, it looks like the framework is choosing to display the action bar on the "right" (or the 2nd fragment), which means the fragment on the left (1st fragment) is missing its action items and menu options.
Everything works fine when the app is in portrait mode, so I'm not sure if I should be doing something to handle the fragments when they are both displayed. Thanks.
EDIT
In each of my fragments I'm using this code to add menu items to the Action Bar:
In fragment 1:
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu_1, menu);
super.onCreateOptionsMenu(menu, inflater);
}
In fragment 2:
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu_2, menu);
super.onCreateOptionsMenu(menu, inflater);
}
UPDATE:
Apparently using setRetainInstance(true) is what caused the menus not to refresh. I was using that because I have an AsyncTask that was throwing an exception if the device was rotated. So I fixed one issue, but broke another.
I think you're thinking about this incorrectly. The action bar is not displayed as part of any fragment, but actually as part of the activity. If you declare in your fragments that you provide action items via setHasOptionsMenu(true), then all will be displayed as part of the action bar. You can then take the appropriate action by overriding onOptionsItemSelected(MenuItem item).

In Android, how to change the title of a different MenuItem inside onOptionsItemSelected()

On clicking a certain MenuItem in a Options Menu in Android, I need to change the title of a different MenuItem in the same Menu. What is the way to do this?
When a MenuItem in an options menu is clicked, onOptionsItemSelected(MenuItem menuitem) is called.
You can change the title of the MenuItem which is passed as a parameter in the
onOptionsItemSelected(MenuItem menuitem), but not of any other MenuItem. I need to know how to change the title of a MenuItem that is not passed as parameter to onOptionsItemSelected(), but which belongs to the same Menu.
I did not find a way to get handle to containing Menu inside the onOptionsItemSelected method (which is called when any MenuItem is clicked).
Many Thanks.
From the Android Dev Guide: http://developer.android.com/guide/topics/ui/menus.html
Changing the menu when it opens
The onCreateOptionsMenu() method is called only the first time the Options Menu is opened. The system keeps and re-uses the Menu you define in this method until your Activity is destroyed. If you want to change the Options Menu each time it opens, you must override the onPrepareOptionsMenu() method. This passes you the Menu object as it currently exists. This is useful if you'd like to remove, add, disable, or enable menu items depending on the current state of your application.
Note: You should never change items in the Options Menu based on the View currently in focus. When in touch mode (when the user is not using a trackball or d-pad), Views cannot take focus, so you should never use focus as the basis for modifying items in the Options Menu. If you want to provide menu items that are context-sensitive to a View, use a Context Menu.
You can do it like this.
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getMenuInflater().inflate(R.menu.photoviewer_menu, menu);
MenuItem item = menu.findItem(R.id.photoviewer_menu_button);
item.setTitle("title");
return super.onPrepareOptionsMenu(menu);
}
This wil work for sure try it............
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addappointmentmenu, menu);
menu.findItem(R.id.add_appontment_menu).setTitle("Add Appointment");
//Similarly set for all your menu's has above
return true;
}
I'm not sure you can change the title, but you could remove the item and then add another one back in with the same ID with a different title.
Menu foo;
foo.removeItem(id);
foo.add(0, id, 0, R.string.a_new_title);
But when MenuItem is more than six times in the Menu resource and if you click on menu then onPrepareOptionsMenu is called either once or twice.
With a simple click on menu then the onPrepareOptionsMenu is called only once
When you click on menu and then click on the "More" option i.e. sixth option then onPrepareOptionsMenu is called twice.
I hope this will help someone.

Categories

Resources