Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android options menu not displaying

I'm new to Android and I've been trying to add a simple add button as mentioned below

list_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:id="@+id/menu_insert"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/menu_insert"              
    />     
</menu>

MyActivity.java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.list_menu, menu);

        return true;  
    }

I read in Dummies series book that ic_menu_add is already there in resources and I don't need to add it, but when I run this code it does not display. I've tried to add a custom icon with same name still there is no button. Can someone help me with it please.

like image 727
user3119647 Avatar asked Jan 19 '14 08:01

user3119647


People also ask

How do I show the menu bar on Android?

This is going to be in res/menu/main_menu . Right click the res folder and choose New > Android Resource File. Type main_menu for the File name. Choose Menu for the Resource type.

Where is the option menu?

In Android, an Option Menu is a set of primary options of an application which users can select one of the options to perform an action. The Option Menu appears on the right side of the App Bar.


1 Answers

If you use a fragment then you need this in onCreate():

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
like image 69
Roel Avatar answered Oct 05 '22 23:10

Roel