How to Add Action Bar Options Menu in Android Fragments

Change actionbar menu in fragment

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

How to change menu items on action bar when fragment changes

As per the guide you can do that

First:
Create a menu file for fragment

Second:
onCreate() method of Fragment set setHasOptionsMenu(true);

Third:
Override onCreateOptionsMenu, where you'll inflate the fragment's menu and attach it to your standard menu.

Fourth:
Override onOptionItemSelected in your fragment for item handlers.

Add Action Button to Action Bar For One Fragment

This is how I solved the problem of having an icon in the action bar for only one page in a view pager.

  1. Create the full menu, including the item that only should be displayed for a particular page, in the main activity the usual way. Save a reference to the menuItem that changes for each page in the Application object.

  2. Implement the onPageChangelistener as above, and set the visibility of the menuItem as needed for each page. You get access to the menuItem from the Application object. This keeps the menu items as you want them in a particular phone orientation, but will fail when the orientation of the phone changes. Save the current page position in the Application object. Be sure to initialize the page position in the Application object to the first page (ie 0).

  3. In the onCreateOptionsMenu in the main activity, use the current page position (found in the Application object) to determine if the menuItem should be visible or not. This will take care of screen rotations.

How to add Options Menu to Fragment in Android

Call the super method:

Java:

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}

Kotlin:

    override fun void onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater)
}

Put log statements in the code to see if the method is not being called or if the menu is not being amended by your code.

Also ensure you are calling setHasOptionsMenu(boolean) in onCreate(Bundle) to notify the fragment that it should participate in options menu handling.

Android How to edit the Action bar menu from Fragment

In your fragment's onCreateView method write

setHasOptionsMenu(true);

And inflate your menu xml file in onCreateOptionsMenu method

In onCreateOptionsMenu of a fragment, write

menu.clear();

before inflating menus

Hide/Show Action Bar Option Menu Item for different fragments

This is one way of doing this:

add a "group" to your menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<group
android:id="@+id/main_menu_group">
<item android:id="@+id/done_item"
android:title="..."
android:icon="..."
android:showAsAction="..."/>
</group>
</menu>

then, add a

Menu menu;

variable to your activity and set it in your override of onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
// inflate your menu here
}

After, add and use this function to your activity when you'd like to show/hide the menu:

public void showOverflowMenu(boolean showMenu){
if(menu == null)
return;
menu.setGroupVisible(R.id.main_menu_group, showMenu);
}

I am not saying this is the best/only way, but it works well for me.

Android - How to access actionbar's menu items in fragment class

Follow this steps:

  • Add setHasOptionsMenu(true) method in your Fragment.

  • Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) and
    onOptionsItemSelected(MenuItem item) methods in your Fragment.

  • Inside your onOptionsItemSelected(MenuItem item) Activity's method,
    make sure you return false when the menu item action would be
    implemented in onOptionsItemSelected(MenuItem item) Fragment's
    method.

Example:

Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Do Activity menu item stuff here
return true;
case R.id.fragment_menu_item:
// Not implemented here
return false;
default:
break;
}

return false;
}

Fragment

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
....
}

@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
// Do something that differs the Activity's menu here
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Not implemented here
return false;
case R.id.fragment_menu_item:
// Do Fragment menu item stuff here
return true;
default:
break;
}

return false;
}


Related Topics



Leave a reply



Submit