Static and dynamic Items in Actionbar - android

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

Related

Fragment not inflating menu after initializing support action toolbar

I have an appcompat activity with no action bar theme. I am running a fragment inside the activity to display a list of items. The items are displayed in two modes: list and delete. When the screen opens, it is always in list mode. The action bars are different in the two display modes. In list mode, in the action bar I have two menu items on the right side which are displayed when the fragment opens for the time. When I come back from the delete mode to list mode, the menu items are not inflated then. How do I make the menu items inflate while coming from delete mode to list mode.
Upon entering the screen, the user goes to the list mode. I have setHasOptionMenu(true); in my fragment. So onCreateOptionMenu and onPrepareOtionsMenu both are called and the two menu items are inflated properly.
When I switch from the list mode to delete mode (which happens when you click on the delete menu item displayed in the action bar on the top-left), I inflate a new custom view and remove all the views from the toolbar and then add this custom view to the toolbar to change the action bar as per the delete mode:
View customView = LayoutInflater.from(getActivity()).inflate(
R.layout.delete_mode_toolbar_layout, null);
Toolbar toolbar = mActionBarHelper.getToolbar();
toolbar.removeAllViews();
toolbar.addView(customView);
And it works totally fine with the delete mode.
But now when I come back to the list mode, I again do the same.
View customView = LayoutInflater.from(getActivity()).inflate(R.layout.list_mode_toolbar_layout,null);
Toolbar toolbar = mActionBarHelper.getToolbar();
toolbar.removeAllViews();
toolbar.addView(customView);
Objects.requireNonNull((AppCompatActivity)getActivity()).supportInvalidateOptionsMenu();
But this time in the list mode, the action bar is coming correctly as per the list mode's custom toolbar, and the oncreateoptionsmenu and onprepareoptionsmenu both are being called. But the menu items are not inflated.
Sorry, but due to some restrictions, this is all the code I can share. Please comment your doubts, I can try to clear them in comments.
So please can someone help me understand why the menu items are not inflating after coming back from the delete mode to list mode.
Thanks in advance.
I got a work around which is a solution to my situation but not the solution to my question.
Now when going from list to delete mode, first hide the list mode custom toolbar layout already present in the toolbar (by keeping a reference to the list mode custom toolbar layout), then inflate the delete mode custom toolbar layout and add it to the toolbar using toolbar.addView(view). Now while coming back to the list mode again from the delete mode, just remove the custom delete toolbar layout from the toolbar using toolbar.removeView(view) and then make the list mode toolbar layout, already present in the toolbar, visible.

Swapping menu items based on current fragment

I've created a NavControllerActivity, which is based on UINavigationController from iOS. I would like the app bar's menu items to only show the menu items for the currently displayed Fragment.
At the moment, as each new Fragment is pushed onto the nav stack, the menu items just get appended to the existing ones. When I tap Back and pop the Fragments off of the nav stack, then those menu items disappear as one would expect.
However, I'd like to hide/remove/something the existing menu items when the new Fragment is pushed on and show just the menu items for that Fragment. Then when that Fragment is popped off, I'd like to remove its menu items and reinstate the menu items from the Fragment that is now at the top of the stack.
I'm currently having each Fragment generate the menu items in onCreateOptionsMenu().
Is there a way to make Android only show the menu items for the top level fragment in my Activity?
Edit: Do I need to have the Activity directly manage the menu items instead? Essentially be constantly invalidating and replacing the menus whenever I push/pop a Fragment? i.e. Still have the menus really defined and controlled within the Fragment, but have the enclosing Activity passing along the menu creation calls to the Fragment at the top of the stack?

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

Altering ActionBar from Fragments

I need to make two minor changes to an Activity's ActionBar from a Fragment.
The parent FragmentActivity's onCreateOptionMenu and onPrepareOptions handle the menu across the app.
However, in the containing main Fragment view, I want the ActionBar title to change for the sake of navigation. Each time the Fragment changes.
Also, in one instance, I want to add a single menu item as defined in the layout. I rather not duplicate code for this part, do I have to override the same methods and set up entire menu again, including onOptionSelected? Or...?
Edit:
On my menu xml layout, I have this item:
<item
android:id="#+id/Add"
android:title="Add Items"/>
This item needs to be seen in the ActionBar only for one particular Fragment. The rest of the menu is identical to what should be seen for the rest of the app. In the fragment where I need this new item, do I have to also do onCreateOptionsMenu, onPrepareOptionsMenu, and onOptionsItemSelected all over again and duplicate thee OTHER menu items as well? Or can I override those in the Fragment and just pop that one extra menu item in?
1.
getActivity().getActionBar().setTitle();
or
getActivity.getSupportActionBar().setTitle();
more clarification please :)
This answered what I needed for part 2.
Putting this in onActivityCreated:
setHasOptionsMenu(true);
And then adding this too:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
ActivityCompat.invalidateOptionsMenu(getActivity());
MenuItem Add = menu.findItem(R.id.Add);
Add.setVisible(true);
}
The parent Activity sets the R.id.Add as invisible by default.

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.

Categories

Resources