I have an activity contains 3 tabs using fragment. I want to load/change the toolbar menu, when tab move.
public class SpendsFragment extends Fragment {
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
MenuItem menuInicio = menu.add(0, 0, 0, "InĂcio");
menuInicio.setIcon(android.R.drawable.ic_menu_edit);
MenuItem menuBusca = menu.add(1, 1, 0, "Buscar");
menuBusca.setIcon(android.R.drawable.ic_menu_search);
setHasOptionsMenu(true);
super.onCreateOptionsMenu(menu, inflater);
}
}
public class MoneyFragment extends Fragment {
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
MenuItem menuInicio = menu.add(0, 0, 0, "Edit");
menuInicio.setIcon(R.drawable.xyz);
MenuItem menuBusca = menu.add(1, 1, 0, "Delete");
menuBusca.setIcon(R.drawable.abc);
setHasOptionsMenu(true);
super.onCreateOptionsMenu(menu, inflater);
}
}
Related
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
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.
I use Fragments and when I switch to nested Fragment, which implements public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) my menu inflates quantity of times when I get to that nested Fragment. How can I avoid this? I also implement constructor of Fragment with methods:
setRetainInstance(true);
setHasOptionsMenu(true);
When I tried to implement siple solution as:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
if(!isInflated)
{
inflater.inflate(R.menu.contacts_archive_menu, menu);
isInflated = true;
}
super.onCreateOptionsMenu(menu, inflater);
}
but my menu wasn't inflate after the screen rotation.
I solved it simply by clearing menu before ionflating of it:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.call_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Just check the count of menu items. Meaning menu.size()==0 ,no menu items are present,then inflate with layout menu,else don't inflate at all.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menu.size() == 0)
inflater.inflate(R.menu.call_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Just override the invalidateOptionsMenu, example:
private var isMenuCreated = false
...
override fun invalidateOptionsMenu() {
Log.d("invalidateOptionsMenu")
if (!isMenuCreated) {
super.invalidateOptionsMenu()
}
}
...
override fun onCreateOptionsMenu(menu: Menu): Boolean {
...
isMenuCreated = true
return true
}
Use before replace.
fragment = new EditMyProfile();
FragmentTransaction fragmentTransactionEditProfile =getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentTransactionEditProfile.replace(R.id.frame, fragment);
fragmentTransactionEditProfile.commit();
I have a ListFragment and I have to add a menu. This is my code:
listuser_menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/any_option"
android:title="In Context Menu" />
</menu>
My ListFragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View mFooterView;
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1:android.R.layout.simple_list_item_1;
if(getListAdapter()==null){
// init adapter
adapter=new UserArrayAdapter(getActivity(),
MOBILE_OS);
}
else{
adapter.notifyDataSetChanged();
}
// set adapter
registerForContextMenu(getListView());
setListAdapter(adapter);
}
#Override
public void onCreateContextMenu(final ContextMenu menu, final View v,
final ContextMenuInfo menuInfo){
menu.clear();
super.onCreateContextMenu(menu, v, menuInfo);
final MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.listuser_menu, menu);
}
But i can't see my menu..why? How register option menu selected? Thanks!
Adding menus to fragment is possible, use the following code in your Fragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.listuser_menu, menu);
}
Using setHasOptionsMenu will allow your fragment to show a menu.
Use this sample code inside your MainActivity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SubMenu subMenu1 = menu.addSubMenu("Action Item");
subMenu1.add(0, 1, 0, "Sample");
subMenu1.add(0, 2, 0, "Menu");
subMenu1.add(0, 3, 0, "Sair");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_title_share_default);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
Yes, you can add menu to ListFragment, add below code into your ListFragment
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
And after that add Fragment version onCreateOptionsMenu() method, as given below
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_xml, menu);
}
# Sherlock Yiu forgot to add "super.onActivityCreated(savedInstanceState);" into onActivityCreated() method, rest is same.
Also dont forgot to add "android-support-v4.jar" into project libraries.
Above code snippet is working for me.
I can not make the menu items appear on the actionbar. They appears on the menu only.
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Favorites").setIcon(
android.R.drawable.btn_star_big_on);
menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Favorites").setIcon(
android.R.drawable.btn_star_big_on);
return true;
}