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);
--------------
}
Related
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);
}
I have a settings menu item that is generated in MainActivity. All the MainActivity's job is to contain fragments that actually do the job. Those fragments have their own action bar items and they work as expected.
The settings menu item should show/hide, depending on a certain condition that is changing between fragments, so assuming the code is on MainActivity, it should be:
if (condition) menu.findItem(R.id.action_settings).setVisible(false);
else menu.findItem(R.id.action_settings).setVisible(true);
Now I'm wondering where should I place the code because the condition changes between fragments but the actual menu item is on MainActivity.
Your condition might be OK and you can put it in your Activity's onPrepareOptionsMenu().
As you said this gets called just one time when you enter the Activity (with some exceptions).
You need to force the system to recall the Activity onPrepareOptionsMenu() when switching through fragments, and that is done - from anywhere - with the line invalidateOptionsMenu() (or, if necessary, getActivity().invalidateOptionsMenu()).
Good points to paste it might be Fragments onResume(), or any other point at which you think your condition state has changed.
Depending on your libraries, there's also a support method for API<11 called supportInvalidateOptionsMenu().
This should be called each time you want your Menu to change. However, if your fragments just add some items to a base, activity menu, take a look at this.
i think it is gonna work for you
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
For each Fragment I will add Menu Items to provide the user with context related actions. But when switching to another Fragment, the Menu items stayed. So I can end with all options from each fragment in the menu.
How do you clean the menu item to have only the ones set up in the activity menu?
I read about invalidateOptionMenu() and onPrepareOPtionmenu() but I don't really get how they work. What is the correct way to implement it ?
Anyway, how is it that removing fragment-linked menu item when the fragment is not displayed anymore is not native Android behavior?
EDIT if instrucitons not clear enough.
I have one activity supporting Navigation_Mode_Tabs with settings as global menu item.
I start fragA that add itemA1 and itemA2. So I have setting, itemA1, itemA2. So far, so good.
Then I switch to fragB that add its own itemB. HERE, I have settings, itemA1, itemA2, itemB in the menu !
Why itemA1 and itemA2 are stuck? How to remove them?
EDIT 2 : I have try another set up of tabulation from this post and it suddenly menu items seems to be correctly remove !
It's something to see with the use of remove(R.id.container, fragment) versus the onDetach()/onAttach() that Google recommands here
I am looking at this, probably tomorrow I'll update. Please, if you know about this, share :)
you could set the menuinflater on each activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.demo, menu);
return true;
}
You should add TabListener to your tabs and call supportInvalidateOptionsMenu() in onTabSelected() method. It cause call onCreateOptionsMenu(). You should override it in your activity and inflate menu for selected fragment. If you add menu items from code don't forget to clear menu for previous fragment.
My app contains a master/detail flow. If I click a ListViewItem a new Fragment is displayed in the detail flow whith its own menu items in the actionbar. I change them with:
#Override
public void onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.klasse, menu);
}
Default the items are displayed at the right side of the screen. Now I want some items which are allways there (not depending on the fragment) on the left side of the screen. So I want some static items on the left in the actionbar and some which can change dynamicly on the right side. How can i realize that?
Or in other words: One OptionMenu for the Activity where the items are static and on the left side and one OptionMenu for the Fragments which can change dynamicly but all in one ActionBar on the top of the Screen. The Event for the static items I want to handel in the Activity.
You are welcome to implement onCreateOptionsMenu() in your activity to have it add action items to the action bar, in addition to those being contributed by your fragments. You can use things like android:orderInCategory to control the sequencing. However, none of the action items will be "on the left side of the screen", as that is reserved for the home affordance, title, and navigation (e.g., drop-down list).
I found a solution that works like a charm for me with static Items on the left side of the ActionBar. See here:
https://stackoverflow.com/a/15667427/2047987
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.