Creating Options menu in Android
-
12-11-2019 - |
Question
I am trying to create options menu in my Android program. I am using the following code to inflate options menu :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
And my xml code is :
?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/Option1"
android:title="Option1"/>
<item
android:id="@+id/Option2"
android:title="Option2"/>
<item
android:id="@+id/Option3"
android:title="Option3"/>
</menu>
But with this code i am not able to show the options menu in my screen.
Also, i am using the code
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
to make the activity as full screen view. Does this code creates problem in inflating the menu?
Thanks in advance,
Timson
Solution
remove the line super.onCreateOptionsMenu(menu);
from your onCreateOptionMenu. You are actually already providing the menu before inflating it.
OTHER TIPS
Options menu shows up by pressing the Options Menu button at the bottom of the phone
Don't call
super.onCreateOptionsMenu(menu);
as that will return a value before your code is executed.
Use this code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow