Search Wikipedia

Search results

Oct 18, 2013

Option Menu in Android

Creating an option menu


There are different kinds of menu in android.
Today i am gonna  create a option menu.
There are couple of ways of creating option menu.
One  is directly by java code and another is using XML.

But in my case i will use XML because it provides good graphical interface and its easy to handle in compare to java. So i create a new XML file.

Creating a mymenu.xml


mymenu.xml


<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item android:id="@+id/item1" 
        android:title="Option 1" 
        android:icon="@android:drawable/ic_menu_compass"/>
   
    <item android:id="@+id/item2 "
        android:title="Option 2" 
        android:icon="@android:drawable/ic_menu_call"/>
</menu>


mymenu.xml



Then i go to my main java file and i need to respond the menu by overriding the onCreateOptionMenu .
i write this code under that override function :

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

Overriding the onCreateOptionsMenu


Then i run my project and it appears like this:


1 comment:

Did this post help you? Do you have any questions? Drop your thoughts here...

}