Android Option Menu Example

By | June 22, 2013
Android Option Menu is a floating menu which displays a list of menu items on long click of a device menu button.

Lets see an example of how to create option menu

Project Detail

Project Name MenuDemo
Package com.pavan.menudemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

   
Create activity_main.xml containing button view with id=”menu_but” and text=”Menu” (name the menu button) 

file :- activity_main.xml

<RelativeLayout
   xmlns:android=”http://schemas.android.com/apk/res/android”
   xmlns:tools=”http://schemas.android.com/tools”
   android:id=”@+id/layout_id”
   android:layout_height=”match_parent”
   android:layout_width=”match_parent”
 >
</RelativeLayout>

2. XML Layout For Menu

Android allows you to create menu  using simple XML file , All the menu layouts xml  files must be placed in /res/menu  directory. 

Create menu.xml file in res/menu and inside menu.xml file each menu item is assoicated with   id which uniquely identifies each menu item and title which sets the name for menu item

file:- menu.xml
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
     <item android:id=”@+id/red” android:title=”RED”></item>
     <item android:id=”@+id/green” android:title=”GREEN”></item>
     <item android:id=”@+id/blue” android:title=”BLUE”></item>
     <item android:id=”@+id/gray” android:title=”GRAY”></item>
     <item android:id=”@+id/cyan” android:title=”CYAN”></item>
     <item android:id=”@+id/yellow” android:title=”YELLOW”></item>
</menu>

3 Activity

Create a class MainActivity and extend it to activity class and override the onCreate() method of activity class and set the content of the actvity with the above defined xml file by calling setContentView() method and passing xml file name as a parameter to it

                      setContentView(activity_main);


   3.1  Inflating Menu

Now to make a menu.xml file to float up on long click of menu button ,we need override the onCreateOptionsMenu() of activity class and inside this menthod make a call to

                getMenuInflater().inflate(R.menu.menu, menu);

    and pass the menu.xml file name and contextmenu object as a parameter to inflate it

   3.2  Responding Menu Item Click

Now to respond to menu item click we override the onOptionsItemSelected(MenuItem item) of activity class, inside this method we get to know which item is clicked by user by calling getItemId() on menuitem object .

finally the class file would look like this
file :- MainActivity.java

package com.pavan.menudemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

 RelativeLayout ourlayout;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 
  setContentView(R.layout.activity_main);
  ourlayout = (RelativeLayout) findViewById(R.id.layout_id);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  getMenuInflater().inflate(R.menu.menu, menu);

  return true;

 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 
  switch (item.getItemId()) {
      case R.id.red:
                ourlayout.setBackgroundColor(Color.RED);
                break;

      case R.id.green:
                ourlayout.setBackgroundColor(Color.GREEN);
                 break;

      case R.id.blue:
               ourlayout.setBackgroundColor(Color.BLUE);
               break;

      case R.id.gray:
              ourlayout.setBackgroundColor(Color.GRAY);
               break;

       case R.id.cyan:
              ourlayout.setBackgroundColor(Color.CYAN);
              break;

       case R.id.yellow:
               ourlayout.setBackgroundColor(Color.YELLOW);
               break;

   }
  return super.onOptionsItemSelected(item);
 }
}

4. RUN

Android Option Menu

Android Option Menu

More on Menus

  1. Android Context Menu
  2. Android Popup Menu
  3. Android Sliding Menu