I have implemented ActionBar Navigation using Fragment. In my App i have one Activity and rest is in Fragments. In my MainActivity i am implementing menu like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Two Fragments uses Navigation Drawer and in their respected fragments i am inflating menu buttons to sort items.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.sort_button_shops, menu);
}
Now the Problem is if i open the Fragment 1 it works perfectly. When i open fragment 2, it shows 2 button to sort, one from Fragment 1 and the second one from Fragment 2.
I have tried to hide the button but it didn't worked.
Any Help will be Appreciated.
Thanks
When you inflate a new menu you are adding new items to the old Menu object, which is probably not what you intended.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.removeItem(R.id.your_menu_item);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Try using this in onResume() of fragments.
MenuItem item = (MenuItem) findViewById(R.menu.activity_main);
item.setVisible(false);
this.invalidateOptionsMenu();
Related
I have an activity in an android app with fragments displayed using a pager.
One of the three fragments shows a menu item (always displayed as action) and an overflow menu, the two others fragments show in the app bar only the first menu item (but not the overflow menu).
My problem is that when I switch from one tab to another, the menu is not updating smoothly.
Is it possible to add animations to menu inflation ?
Has anyone ever encountered such an issue and how did you handle it ?
Here is the app bar :
And here is how I inflate the menu inside the fragment, of course, the other fragment is inflating another XML file.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.home_menu, menu);
}
Please feel free to ask for more details ;-)
Thanks for helping !
In you fragment please write this line in your oncreateview mehod:
setHasOptionsMenu(true);
and implement this mehod:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_referesh, menu);
super.onCreateOptionsMenu(menu,inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
pageNumber=1;
getWebServiceData1();
return false;
}
return super.onOptionsItemSelected(item);
}
I want to have completely different menu options in different fragment.I followed this post.But my fragment menu is adding with the activity menu.But i don't want to have activity menus in some of my fragments.
In SlidingDrawerActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
In my fragment:
public Friends_Status_Comment_Fragment(){
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_add_comment,menu);
super.onCreateOptionsMenu(menu, inflater);
}
The Activities items are adding with the menu of fragment.How to stop it???
I'm not sure if I underestand your problem - in your fragment you should clear menu and create new one - and don't call super :) something like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
menu.clear();
inflater.inflate(R.menu.menu_add_comment,menu);
}
I am creating android application and I'm trying to respect as much as possible the latest Android usability standards. In particular, I am preparing the user interface using the navigation drawer, and I'm trying to ensure compatibility with 2.1+ Android versions. To appreciate the problem, the project consists of:
A main activity;
A navigation drawer;
Four fragments (with their associated layouts).
The problem I'm experiencing occurs when opening the navigation drawer: although each Fragment has its specific menu, when I open the navigation drawer it is added to the nav drawer menu. I tried in several ways (invalidateOptionMenu(), menu.clear(), manipulating functions isDrawerOpen() and isDrawerClose() and more), but I cannot remove the Fragment's menu when opening the navigationdrawer.
These are some snippets of my code, much of it generated by Android Studio, the IDE I'm using:
In main activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.global, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
where "global" is a simple menu with a classical "ic_action_overflow".
And in my fragments I've:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment1, menu);
}
(It's the same of the other Fragments).
Someone can give me some advice on how to act?
I faced the same problem using the boilerplate code generated by Android Studio and got it working by modifying the menu in NavigationDrawerFragment.onPrepareOptionsMenu() (in my case, I wanted to clear the menu altogether):
#Override
public void onPrepareOptionsMenu(Menu menu) {
if (mDrawerLayout != null && isDrawerOpen()) {
menu.clear();
}
}
This is roughly how the options menu is recreated:
NavigationDrawerFragment, which is generated by the IDE, calls supportInvalidateOptionsMenu() when the drawer is opened or closed.
onCreateOptionsMenu gets called: The hosting activity and each of the added fragments gets a chance to contribute menu items.
onPrepareOptionsMenu gets called: Again, the hosting activity and each of the added fragments get a chance to modify the menu.
The fragments are iterated in the order they were added. There is no way to stop the chain of calls midway in steps 2. and 3.
So the idea is to let NavigationDrawerFragment do last-minute changes to the menu in its onPrepareOptionsMenu and no other fragments.
If you need to let other fragments do something in onPrepareOptionsMenu, then you may have to setup those other fragments so they can determine if the drawer is open or not and change their behavior accordingly. This may mean perhaps adding a isDrawerOpen method to the hosting activity or passing in the drawer identifiers to the fragment like it's done in NavigationDrawerFragment.setup().
In your fragment onCreate, add this :
setHasOptionsMenu (true);
And then hide through onPrepareOptionsMenu. e.g.
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_settings).setVisible(false);
}
If you've implemented the navigation drawer the way Android Studio sets it up for you in its example code with a NavigationDrawerFragment, you should have two xml to start with main.xml (activity wide actionbar menu items) and global.xml (global items). I then added a fragment specific menu which adds items to the "activity menu items" as long as the drawer is closed...
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this activity
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
NavigationDrawerFragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// If the drawer is open, show the global app actions in the action bar.
// See also showGlobalContextActionBar, which controls the top-left area
// of the action bar.
if (mDrawerLayout != null && isDrawerOpen()) {
inflater.inflate(R.menu.global, menu);
showGlobalContextActionBar();
}
super.onCreateOptionsMenu(menu, inflater);
}
and in your fragments add as you've described above
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Add fragment specific action bar items to activity action bar items.
// Activity wide items are in main.xml
// Visible action bar items if navigation drawer is visible/open
// are in global.xml
super.onCreateOptionsMenu(menu, inflater);
if (!mNavigationDrawerFragment.isDrawerOpen()) {
inflater.inflate(R.menu.fragment_menu, menu);
}
}
Check out this answer here: https://stackoverflow.com/a/18135409/2558344. Its basically using a boolean to clear items in the navigation drawer and vice versa. But alternatively, you could declare Menu menu as a private variable in your class and use it as: onCreateOptionsMenu(menu, MenuInflater inflater).
Then check in your fragments onStop(), onPause() if it's displaying items or not, if yes then clear them, like:
if (menu != null)
menu.clear();
I'm using ActionBarSherlock in my project and sometimes need to add one or more items inside the action bar.
At this BaixadosFragment class (that extends SherlockFragment), I'm using the following code and it works fine:
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater)
{
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In this case, I'm adding a refresh button, witch is lonely inside main.xml
BUT I want to do the same at CupomDetalheActivity (though adding a share button), witch extends SherlockFragmentActivity instead. So I'm not able to override "onCreateOptionsMenu" as it has a different signature (below):
//this is inside SherlockFragmentActivity
public final boolean onCreateOptionsMenu(android.view.Menu menu) {
return true;
}
//this is inside SherlockFragment
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Nothing to see here.
}
Whith SherlockFragmentActivity, I don't even see where can I use the inflater to bring up the xml containing the share button...
I appreciate a lot any ideas and suggestions...
[EDIT] This worked, according to DroidT's suggestion:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.share, menu);
super.onCreateOptionsMenu(menu);
return true;
}
Your SherlockFragmentActivity also has an onCreateOptionsMenu() and onPrepareOptionsMenu(). You can inflate your menu options in the onCreateOptionsMenu() by using getSupportMenuInflater(). You would want to make a call to invalidateOptionsMenu() in your SherlockFragmentActivity whenever you want the change to happen and add the menu options in onPrepareOptionsMenu(). For more information look at the "Changing menu items at runtime" section of this link.
If you are using a menu inside of a fragment, make sure you call setHasOptionsMenu(true); in the fragments onCreate(Bundle savedInstance) method
Background
Developing for Android 3.0, I have a HostActivity that is the superclass of NotebooksActivity and NoteActivity. NotebooksActivity includes a fragment, NotebooksFragment.
In HostActivity, I include a menu that I want to appear at the rightmost end of the options menu in the ActionBar, i.e. all menu items in subclasses of HostActivity should appear to the left of the menu items added in HostActivity.
Menu inflation in HostActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.host_menu, menu);
return super.onCreateOptionsMenu(menu);
}
The Problem
When I add menu items in NoteActivity, I achieve the desired order as expected:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.notebook_menu, menu);
return super.onCreateOptionsMenu(menu);
}
However, when I add menu items in NotebooksFragment, because of how Fragments work, onCreateOptionsMenu is called after the same method in HostActivity, resulting in HostActivity's menu items appearing before NotebooksFragment's.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.notebooks_menu, menu);
SearchView sv = new SearchView(getActivity());
sv.setOnQueryTextListener(this);
menu.findItem(R.id.search_notebooks).setActionView(sv);
super.onCreateOptionsMenu(menu, inflater);
}
How can I achieve my desired menu ordering?
You can try using android:menuCategory and android:orderInCategory to more manually specify your ordering.
Or, use onPrepareOptionsMenu() in HostActivity, as that is called after onCreateOptionsMenu(). Downside is that it is called on every MENU button press, not just the first time.