Option Menu in Android

Introduction

 
We have seen a "Menu" button on an Android screen, but how is it useful and how can we create a menu in android application? This article will help you to do the same.
 
Building Menus in an Android Application
 
Menu is generally used to  give extra functionality to an application. One perfect example is, while we are making a phone call, we may want to see other contacts or messages. This is where the Option Menu comes in picture. This can be done using the same.
 
Menu has 3 types:
  1. Option Menu (Visible if touch "Menu" button)
  2. Context Menu (Visible if long press on any View. View is anything like Button, EditText)
  3. Sub Menu
The following tutorial will guide you towards creating this kind of application step by step.
 
Step 1: Add "optionmenu.xml"
 
Right Click on "project" -> New -> Andorid Xml File
 
index.jpeg
 
 
Step 2: Edit "optoinmenu.xml"
 
This file contains items to be displayed. It takes the "id" that uniquely identify that item and the "title" that displays on  screen.
 
To edit that file
 
Your Project -> res -> menu -> optionmenu.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <item android:id="@+id/Color" android:title="Color">  
  5.            <menu>  
  6.             <item android:id="@+id/RedColor" android:title="Red"/>  
  7.             <item android:id="@+id/GreenColor" android:title="Green"/>  
  8.         </menu>  
  9.    </item>  
  10.     
  11. </menu> 
Step 3: Edit "MenuOptionDemoActivity.java"
  1. package com.MenuOptionDemo;    
  2.     
  3. import android.app.Activity;    
  4. import android.os.Bundle;    
  5. import android.view.Menu;    
  6. import android.view.MenuInflater;    
  7. import android.view.MenuItem;    
  8. import android.widget.Toast;    
  9.     
  10. public class MenuOptionDemoActivity extends Activity {    
  11.     /** Called when the activity is first created. */    
  12.     @Override    
  13.     public void onCreate(Bundle savedInstanceState) {    
  14.         super.onCreate(savedInstanceState);    
  15.         setContentView(R.layout.main);    
  16.     }    
  17.        
  18.     @Override    
  19.     public boolean onCreateOptionsMenu(Menu menu) {    
  20.         // TODO Auto-generated method stub    
  21.         MenuInflater inflater=getMenuInflater();    
  22.         inflater.inflate(R.menu.optionmenu, menu);    
  23.            
  24.         return super.onCreateOptionsMenu(menu);    
  25.     }    
  26.        
  27.     @Override    
  28.     public boolean onOptionsItemSelected(MenuItem item) {    
  29.         // TODO Auto-generated method stub    
  30.         if(item.getItemId()==R.id.RedColor)    
  31.         {    
  32.             Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();    
  33.         }    
  34.         else if(item.getItemId()==R.id.GreenColor)    
  35.         {    
  36.             Toast.makeText(MenuOptionDemoActivity.this"Green Color Selected"1000).show();    
  37.         }    
  38.         return super.onOptionsItemSelected(item);    
  39.     }    
  40. }  
    In this java file, "onCreateOptionMenu" used to create option menu and "onOptionsItemSelected" used to listen which item is selected by user.
     
    Step 4: Start Emulator
     
    index1jpeg.jpeg
     
    index2.jpeg
     
    index3.jpeg
     

    Summary

     
    In this brief tutorial, we discussed how to create menus in an Android application.


    Similar Articles