Reputation:
My requirement is simple, I want to open list of menuItems in fragment. So I created menu XML inside menu and trying to inflate inside onCreateOptionsMenu method in Fragment but unfortunately nothing is showing.
Here is:
myFragment.java
public static MoreFragment newInstance() {
return new MoreFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.more_drawer, menu);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setHasOptionsMenu(true);
View v = inflater.inflate(R.layout.fragment_more, container, false);
setUp(v);
return v;
}
here is, more_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_busTracking"
android:icon="@drawable/ic_more_drawer_bus_tracking"
android:title="Bus Tracking" />
<item
android:id="@+id/nav_kidsWallet"
android:icon="@drawable/ic_more_drawer_kids_wallet"
android:title="Kids' Wallet" />
<item
android:id="@+id/nav_circlals"
android:icon="@drawable/ic_more_drawer_circulars"
android:title="Circulars" />
<item
android:id="@+id/nav_message"
android:icon="@drawable/ic_more_drawer_messages"
android:title="Messages" />
<item
android:id="@+id/nav_accountSettingd"
android:icon="@drawable/ic_more_drawer_account_settings"
android:title="@string/account_settings" />
<item
android:id="@+id/nav_logout"
android:icon="@drawable/ic_more_drawer_logout"
android:title="Log Out" />
</menu>
fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
</RelativeLayout>
I tried setHasOptionsMenu(true) put inside onCreate() and onCreateView() but nothing is showing.
I tried debug and find that onCreateOptionsMenu() is not called. I tried all the solution provided to this question but nothing goes in my favor.
What am I doing wrong?
Upvotes: 1
Views: 6858
Reputation: 11
Have you checked your Manifest file?
Make sure the Theme you choose for the activity includes the action bar. e.g:android:theme="@style/Theme.AppCompat">
Upvotes: 1
Reputation: 16976
Change this:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.more_drawer, menu);
}
To:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.more_drawer, menu);
super.onCreateOptionsMenu(menu, inflater);
}
And use: setHasOptionsMenu(true);
after view inflates-creates:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
Upvotes: 5