Creating Options menu in Android

6.3k Views Asked by At

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

4

There are 4 best solutions below

7
On BEST ANSWER

remove the line super.onCreateOptionsMenu(menu); from your onCreateOptionMenu. You are actually already providing the menu before inflating it.

1
On

Options menu shows up by pressing the Options Menu button at the bottom of the phone

0
On

Don't call

super.onCreateOptionsMenu(menu);

as that will return a value before your code is executed.

1
On

Use this code:

public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.options_menu, menu);
      return true;
    }