Reputation: 7517
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.
Upvotes: 2
Views: 1488
Reputation: 172
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);
}
}
Upvotes: 0
Reputation: 39191
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.
Upvotes: 5
Reputation: 485
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;
}
});
Upvotes: 4
Reputation: 290
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);
}
Upvotes: 0