ListFragment - How do I add a menu? - android

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.

Related

Android - onCreateOptionsMenu - Can't disable MenuItem from fragment

I'm trying to disable the MenuItem's in my navigationdrawer from my
fragment, but it just wont work...
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.waiting_for_terminal, container, false);
setHasOptionsMenu(true);
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activity_main_drawer, menu);
menu.findItem(R.id.nav_amount).setEnabled(false);
menu.findItem(R.id.nav_return).setEnabled(false);
menu.findItem(R.id.nav_about).setEnabled(false);
menu.findItem(R.id.nav_settings).setEnabled(false);
super.onCreateOptionsMenu(menu, inflater);
}
I can call getTitle() for the MenuItems, and it will return correct value. But for some reason setEnabled(), setTitle(), setVisible() etc. does not work, the value stays the same...
to disable menuitem in fragment use it with fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
Remove onCreateOptionsMenu() inside the Activity,and use inside a fragment as:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
Try This:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
oldDescription= ActivityConstantUtils.sBlogDescriprtion;
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_act_add_section, menu);
MenuItem item = menu.findItem(R.id.action_preview);
item.setIcon(null);
item.setTitle("");
super.onCreateOptionsMenu(menu, inflater);
}
I think you are trying to disable that Home Button on the AppBar which toggles the NavigationDrawer.
The best way i can think of doing that is:
In Method onOptionItemSelected:
protected onOptionItemSelected(MenuItem item)
{
if(item.getItemId() == android.R.id.home)
{
// do anything you want here
}
}
This will help you override that Home Button.
More over if you want to replace that Hamburger Icon with an Default Arrow Icon, you can use
mNavigationDrawer.setDrawerIndicatorEnabled(false);
Additionally if you want to disable the drawer Swipe Functionality also,
you can use
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Hope it Helps. :)
Omg, my bad...
It was MenuItems in the NavigationView I wanted to disable...
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().findItem(R.id.nav_amount).setEnabled(false);
Thanks for the help though :)

Change actionbar menu in fragment

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

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

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