Simple Options Menu in Android

23 Jan
  1. Create an Android XML file of Menu type
  2. Add menu options as you preferred. Each menu option should be indicated as <item> XML element.
  3. This is my options.xml file.
    <!--?xml version=<em--><?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="" >
    <item android:id="@+id/item1" android:title="Edit" android:icon="@android:drawable/ic_menu_edit"></item>
    <item android:id="@+id/item2" android:title="Delete" android:icon="@android:drawable/ic_menu_delete"></item>
    <item android:id="@+id/item3" android:title="Help" android:icon="@android:drawable/ic_menu_help"></item>
    <item android:id="@+id/item4" android:title="More" android:icon="@android:drawable/ic_menu_more"></item>
  4. My options menu is not just text-based. Each menu item is displayed with a relevant icon. Here I used the icons found in the Android.jar file of my project. You can find such icons using this path
    <app root > –> Android <Your Android platform>  –> android.jar –> res –> drawable-hdpi(icons for menu prefixed with ic_menu)
  5. Override onCreateOptionsMenu and onOptionsItemSelected in your file
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    public class OptionMenuActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflator = getMenuInflater();
    inflator.inflate(, menu);
    return true;
    public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() ==
    Log.d("Edit", "Selected : Edit Option");
    if(item.getItemId() ==
    Log.d("Delete", "Selected : Delete Option");
    return super.onOptionsItemSelected(item);
Leave a comment

Posted by on January 23, 2012 in Android



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: