Context Menu in Android

Introduction

 
If you have been writing software applications for a while, I am sure you are familiar with context menus. A context menu is a list of action items popup when you right click on a screen in an application.
 
In an Android phone, if we long touch EditText, it will open a list of items and when you click on these items, there are some actions corresponding to them. This is known as a Context Menu in Android. Context Menu is generally helpful while you want to give some specific additional functionality to your Views. Views are nothing but application's EditText, Buttons etc.
 
In this step by step tutorial, we will see how to build a context menu based Android application.
 
Step 1: Create "mymenu.xml"
 
Right Click "your project" -> new -> Android XML file
 
CntAnd1.jpeg
 
Step 2: Edit your "mymenu.xml" file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   <group android:checkableBehavior="single">  
  5.     <item android:id="@+id/CutText" android:title="Cut"></item>  
  6.     <item android:id="@+id/CopyText" android:title="Copy"></item>  
  7.     <item android:id="@+id/PasteText" android:title="Paste"></item>  
  8.    </group>  
  9. </menu> 
     
Step 3: Edit your "ContextMenu.java" file
 
Before start editing, let's have a look at the Context menu work. A context menu generally works on a View. For that, first, we need to "register" our view (or views) that will display the Context Menu on long touch. This is done by using "registerForContextMenu". It takes a View as an argument.
  1. import android.view.ContextMenu;  
  2. import android.view.ContextMenu.ContextMenuInfo; 
Used to Additional information regarding the creation of the context menu
 
CContextMenu.java
  1.  package com.CContextMenu;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.ContextMenu;  
  5. import android.view.MenuInflater;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.view.ContextMenu.ContextMenuInfo;  
  9. import android.widget.EditText;  
  10. import android.widget.Toast;  
  11. public class CContextMenuActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         EditText ed=(EditText) findViewById(R.id.editText1);  
  18.         registerForContextMenu(ed);  
  19.       //You can write multiple register statement as per your need  
  20.     }  
  21.       @Override  
  22.       public void onCreateContextMenu(ContextMenu menu, View v,  
  23.                   ContextMenuInfo menuInfo) {  
  24.             // TODO Auto-generated method stub     
  25.             MenuInflater inflater=getMenuInflater();  
  26.             inflater.inflate(R.menu.mymenu, menu);  
  27.             super.onCreateContextMenu(menu, v, menuInfo);  
  28.       }  
  29.       @Override  
  30.       public boolean onContextItemSelected(MenuItem item) {  
  31.             String str="";  
  32.             if(item.getItemId()==R.id.CutText)  
  33.                   str= "Cut";  
  34.             else if(item.getItemId()==R.id.CopyText)  
  35.                   str= "Copy";  
  36.             else if(item.getItemId()==R.id.PasteText)  
  37.                   str= "Paste";  
  38.             Toast.makeText(CContextMenuActivity.this, str,1000).show();  
  39.             return super.onContextItemSelected(item);  
  40.       }  
OnCreateContextMenu have 3 arguments
  1. ContextMenu -> Used to build Context Menu.
  2. View -> The view for which the context menu is being built.
  3. ContextMenuInfo -> Extra information about the item for which the context menu should be shown.
OnContextItemSelected have 1 argument
  1. MenuItem -> Returns menu item selected from Context Menu.
Step 4: Start Emulator
 
CntAnd2.jpeg
 
CntAnd3.jpeg
 
CntAnd4.jpeg
 

Summary

 
Context Menu is useful to give additional features to our application.
 
Resources
 
Here are some useful related resources:


Similar Articles