Change actionbar menu in fragment - android

I want to load another menu xml when I load the fragment.I am using this code in main activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
I am using this code in fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
When user loads the fragment,activity menu should remove then fragment menu should load to actionbar.
And when user clicks the back button from fragment,fragment menu should remove then main activity menu should load to action bar.
Now this code is not removing the old menu,it's adding new menu to near of old menu.
How can I do this ?

You Can use menu.clear(); method.
It remove all existing items from the menu, leaving it empty as if it had just been created.
Try this..
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
menu.clear(); // clears all menu items..
getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}

You Should call setHasOptionsMenu(true); method in onCreateView.
It tells fragment got a menu.
Then Try below codes...
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_manage, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
menu.clear(); // clears all menu items..
inflater.inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}

Try this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
Hope it helps

Related

Trying to hide/disable entire menu (overflow) in fragment

I have been unsuccessful in trying to hide or disable the overflow menu in one fragment.
I have tried setting setHasOptionsMenu(false) with no success, and then I tried setHasOptionsMenu(true) and tried inflating with an empty menu like below.
Both attempts do not work for me.
How do I hide or disable the options/overflow menu in one fragment only??
Thanks in advance!
Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.empty, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Figured out how to do it. Just need to set the menu items I don't want visible. In my case, I set all of them not visible and that removed the overflow menu entirely.
Also don't forget you have to setHasOptionsMenu(true) in your onCreate so it knows to call onCreateOptionsMenu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menu != null) {
menu.findItem(R.id.menu_settings).setVisible(false);
menu.findItem(R.id.menu_leave_feedback).setVisible(false);
menu.findItem(R.id.menu_shop).setVisible(false);
}
}
I know this answer is very late but I like to share it.
In Fragment onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
And in Fragment onCreateOptionsMenu() method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
}
setHasOptionsMenu(true)
in OnCreate() and
#Override
public void onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
menu.findItem(R.id.xxx).setVisible(false);
menu.findItem(R.id.yyy).setVisible(false);
}

Menu items always getting cleared

I have a viewpager with some fragments.
For example I have 2 fragments with different menu items.
Fragment A (menu)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
manager = (SearchManager) getActivity().getSystemService(getActivity().getApplicationContext().SEARCH_SERVICE);
sv = (SearchView)menu.findItem(R.id.search).getActionView();
sv.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
sv.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
pAdapter.filter(newText.toLowerCase());
return true;
}
});
}
Fragment B (no menu , clear)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
}
However when launching the menu items are gone. If I comment menu.clear(); menu items appears but all the fragments have the same menu items..
For those two fragments I call in onCreate setHasOptionsMenu(true);
How can I use different menu layouts with different fragments ?
Thanks in advance
I think you should use
inflater.inflate(R.menu.main, menu);
with different menu in every fragment.

Error in onCreateOptionsMenu at detail activity

I have a listview and call detail activity when the item is selected.
My onCreateOptionsMenu has error on displaying the menu at Action Bar.
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getActivity()).inflate(R.menu.detail_view_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
The error is The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu). Error happened at return line.
I implement listview and detail activity using fragmentTransaction.
Thanks
Your onCreateOptionsMenu(Menu menu) just needs to be in the activity that hosts the fragment, not in the fragment itself.
You could also consider extending a BaseActivity and including it in there just once.
public class BaseActivity extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getActivity()).inflate(R.menu.detail_view_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
}
public class ListActivity extends BaseActivity {
// ...
}
public class DetailActivity extends BaseActivity {
// ...
}
Try it like this but put it in your main Activity class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.detail_view_menu, menu);
return true;
}
OR if you want your Fragment to add items to the ActionBar you have to use a slightly different construct:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.detail_view_menu, menu);
}
You have an additional parameter that you have to add(MenuInflater). Also, in a Fragment the onCreateOptionsMenu doesn't return a boolean value.
Now that you have your inflater you need to call setHasOptionsMenu(true) in your Fragment's onCreate() method. Otherwise your items will not be displayed in the ActionBar.
Your Fragment code, handling the menu inflation, should now look like this:
public class DetailFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.detail_view_menu, menu);
}
}

invalidateOptionsMenu doesnt get called from fragment

I have a fragment with that needs to build its own action bar :
public class CalendarFragment extends Fragment {
public CalendarFragment() {
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().supportInvalidateOptionsMenu();
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.calendar_menu1, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText("Calendar Fragment");
return textView;
}
}
the problem is it doesnt create a new menu with items from calendar_menu1 but just adds the items from it to the old menu, as if invalidateOptionsMenu doesnt work (i tried getActivity().invalidateOptionsMenu() too)
You must call in onCreate():
setHasOptionsMenu(true);
It is normal, looking into the javadoc of the MenuInflater, "The items and submenus will be added to this Menu":
public void inflate (int menuRes, Menu menu)
Inflate a menu hierarchy from the specified XML resource. Throws InflateException if there is an error.
Parameters
menuRes Resource ID for an XML layout resource to load (e.g., R.menu.main_activity)
menu The Menu to inflate into. The items and submenus will be added to this Menu.
Did you try to call menu.clear() before to inflate your fragment menu?
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.calendar_menu1, menu);
}

Can I have different menu for each tab of TabHost

Is that possible to have different menus for each tab of TabHost?
Yes, you can in onCreateOptionsMenu depending on the tab inflate a different menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
int tab = getTabHost().getCurrentTab()
if (tab==1)
inflater.inflate(R.menu.main_menu, menu);
else
inflater.inflate(R.menu.other_menu, menu);
return true;
}
You need to provide different versions of menu.xml files within res/menu for this.
If you're using fragments you can put setHasOptionsMenu(true); inside onCreate method of your fragment and override onCreateOptionsMenu.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_xxx, menu);
}

Categories

Resources