Search code examples
androidandroid-fragmentsandroid-menuoncreateoptionsmenu

Unable to set menu for fragment


Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    Log.i("frag", "onCreate");
}


 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    final View view = inflater.inflate(R.layout.fragment_initiate_chat, container, false);

    bindViews(view);

    setUpClickListener();

    setUpCategories(view);

    return view;
}

 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //menu.clear();
    inflater.inflate(R.menu.menu_inititate_chat, menu);
    Log.i("frag", "onCreateOptionsMenu");
    //super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i("frag", "onOptionsItemSelected");
    final int itemId = item.getItemId();
    switch (itemId) {
        case R.id.send:
            sendIssue();
            break;

    }
    return super.onOptionsItemSelected(item);
}

I am unable to set a menu for my fragment.I have used the setHasOptionsMenu(true); but it still doesn't make a difference.

My fragment xml layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgGrey"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:navigationIcon="@drawable/ic_close_black_24dp"
    app:popupTheme="@style/Theme.AppCompat.NoActionBar"
    app:titleTextColor="@color/toolbarTextColor" />

<Spinner
    android:id="@+id/categories"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


    </LinearLayout>`

The menu item menu_inititate_chat.xml

    `<?xml version="1.0" encoding="utf-8"?>
     <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
   <item
    android:id="@+id/send"
    android:orderInCategory="100"
    android:title="@string/send"
    android:icon="@drawable/send"
    app:showAsAction="always" />
    </menu>`

The activity which contains this fragment does not included a toolbar of its own.


Solution

  • The standard Options Menu only works with an ActionBar, or the support version thereof.

    If you want a menu to show on an arbitrary Toolbar, you can just put it there yourself. The Toolbar#inflateMenu() method can replace the menu inflation you currently have in onCreateOptionsMenu(), and the Toolbar#setOnMenuItemClickListener() method can set a listener to replace the onOptionsItemSelected() method's function.

    Alternatively, you could try setting the Toolbar as the Activity's support ActionBar, if your design allows, and stick with the standard Options Menu setup, though that's kinda clunky, and might be a little error-prone.