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

Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
  • 1
    The standard options menu only works on an `ActionBar`, or the support version thereof. If you want a menu to show on an arbitrary `Toolbar`, you'll have to put it there yourself. Have a look at the `Toolbar#inflateMenu()` and `Toolbar#setOnMenuItemClickListener()` methods. – Mike M. Feb 20 '17 at 07:12
  • 1
    @MikeM. yep that was the one, could you write the same as an answer so i can mark it correct. – Jude Fernandes Feb 20 '17 at 07:15

4 Answers4

5

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.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
4

if you are using ToolBar in your Fragment ,then you can do it this way:

 toolBar.inflateMenu(R.menu.delete_address_menu);
 toolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return false;
        }
    });
Du.Fantasy
  • 475
  • 2
  • 7
  • thanks but another guy answered it first so i had to mark his as the right answer but this is exactly what needs to be done. – Jude Fernandes Feb 20 '17 at 19:25
0

Use this.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub

    inflater = getActivity().getMenuInflater();

    inflater.inflate(R.menu.menu_inititate_chat, menu);


}
Rajkumar Kumawat
  • 290
  • 2
  • 11
0

Try to change your onOptionsItemSelected like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i("frag", "onOptionsItemSelected");
    final int itemId = item.getItemId();
    switch (itemId) {
        case R.id.send:
            sendIssue();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
gotwo
  • 663
  • 8
  • 16
Saloni
  • 172
  • 2
  • 15