How to add Options Menu to Fragment in Android

android fragment options menu not showing
android fragment menu item click
android add menu to actionbar
android menu bar
android menu icon
android options menu
android overflow menu
floating context menu android

I am trying to add an item to the options menu from a group of fragments.

I have created a new MenuFragment class and extended this for the fragments I wish to include the menu item in. Here is the code:

public class MenuFragment extends Fragment {

    MenuItem fav;

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

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        fav = menu.add("add");
        fav.setIcon(R.drawable.btn_star_big_off);
    }
}

For some reason the onCreateOptionsMenu appears not to run.

Call the super method:

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

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 Fragment Menu Example, Why do you need to add action items from Fragments? In the above image shows that the menu contains common items like Search, Settings,  Create the menu files, as usual, then you need to specify that the fragment contains menu using the below snippet. Now you need to inflate the menu for this fragment using OnCreateOptionsMenu, just override this native method into your fragment and initialize the menu.

I had the same problem, but I think it's better to summarize and introduce the last step to get it working:

  1. Add setHasOptionsMenu(true) method in your Fragment's onCreate(Bundle savedInstanceState) method.

  2. Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) (if you want to do something different in your Fragment's menu) and onOptionsItemSelected(MenuItem item) methods in your Fragment.

  3. 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.

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

How to use fragment specific menu in android ?, This Video will cover the followings ✓Use Options Menu / Actionbar menu ✓Use Options Menu in Duration: 7:05 Posted: Feb 3, 2019 Report that this fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and related methods. If hasMenu is true, the fragment has menu items to contribute. To hide a particular MenuItem you can call setVisible(false) on it in onPrepareOptionsMenu(). For example:

If you find the onCreateOptionsMenu(Menu menu, MenuInflater inflater) method is not being invoked, make sure you call the following from the Fragment's onCreate(Bundle savedInstanceState) method:

setHasOptionsMenu(true)

Options Menu Fragment - Android Studio - Java, . Now open newly created xml (popup_menu. xml) file and write the code like as shown below. How To Add Menu Items For Android Fragment. Create a fragment menu items layout xml file. You can read article Android Actionbar Example for more detail. Override Fragment class onCreate(@Nullable Bundle savedInstanceState) method, set hasOptionsMenu to true. @Override public void onCreate(@Nullable

If you need a menu to refresh a webview inside a specific Fragment, you can use:

Fragment:

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // TODO Add your menu entries here
    inflater.inflate(R.menu.menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.exit:
        System.exit(1);
        break;

    case R.id.refresh:
        webView.reload();
        break;
    }
    return true;

}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/exit" android:title="Exit" android:icon="@drawable/ic_action_cancel" />
    <item android:id="@+id/refresh" android:title="Refresh" android:icon="@drawable/ic_action_refresh" />
</menu>

Menus, items for an activity and it is useful to implement actions that have a global impact on the app, such as Settings, Search, etc. To add a menu for each fragment, you should go through many steps: 1) First of all, add setHasOptionsMenu(true) in the fragment's onCreateView() like below: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { setHasOptionsMenu(true); .

In the menu.xml you should add all the menu items. Then you can hide items that you don't want to see in the initial loading.

menu.xml

<item
    android:id="@+id/action_newItem"
    android:icon="@drawable/action_newItem"
    android:showAsAction="never"
    android:visible="false"
    android:title="@string/action_newItem"/>

Add setHasOptionsMenu(true) in the onCreate() method to invoke the menu items in your Fragment class.

FragmentClass.java

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

You don't need to override onCreateOptionsMenu in your Fragment class again. Menu items can be changed (Add/remove) by overriding onPrepareOptionsMenumethod available in Fragment.

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.action_newItem).setVisible(true);
    super.onPrepareOptionsMenu(menu);

}

Android Popup Menu with Examples, ✓Add items with icons in options menu ✓Show different options of an options menu in each Duration: 7:05 Posted: Feb 3, 2019 7 Answers 7. Add similar code to your fragments: This way you can customize the menu for your fragments. This is the correct answer because the call to setHasOptionsMenu(true) is required for the onCreateOptionsMenu method being called in the fragment.

Android Options Menu with Examples, As Android programming goes, creating an options menu item/button in Note that in this tutorial I assume that you're using a Fragment with  As Android programming goes, creating an options menu item/button in the Android ActionBar is fairly straightforward. In this brief tutorial I’ll demonstrate all of the steps needed to add a new menu item to the action bar. Note that in this tutorial I assume that you’re using a Fragment with your Activity.

Options Menu Fragment - Java, Create a new Android project, and setup the layout for MainActivity in order to add the fragment: <?xml version="1.0" encoding="utf-8"?>  How can I change option menu in different fragments? Ask Question Asked 5 years, 8 months ago. How to add Options Menu to Fragment in Android. 729.

Android ActionBar example: How to create an options menu item , If both activity and fragment declare items for the options menu they are combined If you want to add menu items to one of the descendant activities, override  Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. Here, we are going to see two examples of option menus. First, the simple option menus and second, options menus with images. Here, we are inflating the menu by calling the inflate () method of MenuInflater class.

Comments
  • maybe a silly question... you press the menu button right?
  • ..lol...yes I have pressed the menu button, I have also tried it with and without: fav.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  • Hi, maybe this thread will help you or check the api demo for a working example.
  • droidmentor.com/how-to-use-fragment-specific-menu-in-android
  • Thanks for the help, I added the super method, and realised I had removed the @Override so added this back in, eclipse threw an error so I replaced the MenuInflater to import android.view.MenuInflater; instead of import android.support.v4.view.MenuInflater; and now all is working
  • not calling setHasOptionsMenu(true) in the Fragment's onCreate has gotten me twice now
  • I was transferring my Activity to a Fragment and ran into this issue. The method signature has changed from public boolean to public void and the arguments have also changed. Make sure to take note of this!
  • Note that Fragment.onCreateOptionsMenu(Menu, MenuInflater) is an empty method, so calling super is not required at all. The only mistake here was the wrong method signature and possibly a missing setHasOptionsMenu() in onCreate()
  • Replace super.onCreateOptionsMenu with inflater.inflate(R.menu.menu_custom, menu);
  • This must,must,must be the accepted answer. Help me after spending 5 hours:)
  • Just was missing setHasOptionsMenu(true); Thanks
  • On a viewPager on some devices the menu is only shown the second time the you go to the fragment
  • @Marco HC your code works fine. But what if i want to hide some of the menu for Activity or for Fragment? You have mention about where to implement which option menu (In activity and fragment). But what if i want to hide some menu?