Browse By

Option Menu in Android

Almost every application will need a menu in order to facilitate a user to perform actions on the application. In Android, there are three types of menus possible.

  1. Options Menu
  2. Context Menu
  3. Popup Menu

The Option Menu is the one that appears when user touches the menu button on the mobile. This is something that is associated with with the activity. In 3.0 and later, this is available in the Action Bar itself for quick access.

Launch Option Menu by clicking on Menu Button

Launch Option Menu by clicking on Menu Button

There are two ways of creating an Options Menu in your application. One is by instantiating the Menu class and the other is by inflating a Menu from an XML menu resource.  Based on best practices it is always better to define the Menu in an XML and inflate it in your code.

In this tutorial, we will create simple android application which will display Option Menu by inflating Menu from XML menu resource.  It will display four menu items as follows; Add Record, Edit Record, Delete Record and Save Record  when some one click on Menu Button.

Direct Download Full Source Code



Option Menu in Android

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2. Now Create option_menu.xml file in Menu Resource. It will be created in res/menu folder. Put below code in that file
    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="">
    	<item android:title="Add Record" android:icon="@android:drawable/ic_menu_add"
    	<item android:id="@+id/btnEdit" android:title="Edit Record"
    	<item android:id="@+id/btnDelete" android:title="Delete Record"
    	<item android:id="@+id/btnSave" android:title="Save Record"
  3. Open your main.xml from (res/menu folder) and design a simple layout which display message like “Click Menu Button to launch Option Menu “.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:orientation="vertical" android:layout_width="fill_parent"
    	<TextView android:textColor="#fff" android:textSize="20dp"
    		android:layout_height="wrap_content" android:id="@+id/textView1"
    		android:text="Pulse 7 Demo" android:layout_width="wrap_content"
    	<TextView android:textColor="#fff" android:textSize="15dp"
    		android:layout_height="wrap_content" android:id="@+id/textView2"
    		android:text="Click Menu Button to launch Option Menu "
    		android:layout_width="wrap_content" android:layout_gravity="center_horizontal"
  4. Now open your main Activity class file and override methods ;  onCreateOptionsMenu(Menu menu) for inflating option menu in activity, and onOptionsItemSelected(MenuItem item) to perform action when some one click on option menu item.
    package com.vrs.optionmenu;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.Toast;
    public class Pulse7OptionMenuActivity extends Activity {
    	/** Called when the activity is first created. */
    	public void onCreate(Bundle savedInstanceState) {
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate Option menu from XML Resource and display on activity.
    		MenuInflater menuInflater = getMenuInflater();
    		menuInflater.inflate(, menu);
    		return true;
    	public boolean onOptionsItemSelected(MenuItem item) {
    		// Perform Action when user click on menu item
    		switch (item.getItemId()) {
    					"Record Added Successfully", Toast.LENGTH_LONG).show();
    			return true;
    					"Record Edited Successfully", Toast.LENGTH_LONG).show();
    			return true;
    					"Record Deleted Successfully", Toast.LENGTH_LONG).show();
    			return true;
    					"Record Saved Successfully", Toast.LENGTH_LONG).show();
    			return true;
    			return super.onOptionsItemSelected(item);
  5. Now Execute Application
    • Click on Menu button of keyboard
    Option Menu in Android

    Clicking on Menu, it will display Option on Screen

    Android Option Menu Example

    Perform action when Menu item is clicked.

Points to Remember

  •  onCreateOptionsMenu(Menu menu) method need to override for inflating option menu from XML resource and display on screen.
  • getMenuInflater() method use for getting MenuInflater class object
  • inflate()  method of MenuInflater class, inflate your xml resource .
  • onOptionsItemSelected(MenuItem item) method need to override for performing action on each item of menu.

I hope you like this article. Share your views to to improve content.

Download Full Source Code