Android context menu appears when the user press long click on the element. It is also known as floating menu. Context Menu is a useful solution, particularly when we want to provide a list of actions based on an item click in a ListView or other AdapterView.



1.       Ellipse SDK

2.       Android SDK

3.       ADT plugin

Or Android Studio and a compatible version of JAVA SDK

Install and configure the above utilities.

Now create a new Android project namely “ContextMenusample”.

Creating a Menu Layout:

For this, navigate to res/menu/ from package explorer in Eclipse and create an xml

to define the menu itself. We’ll call this one contextmenu.xml Open


Add following code into it:


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="" >
        android:title="Delete Item"/>     <item
        android:title="Edit Item"/>
 Main Activity Class Implementation: 

Now, navigate to res/src/MainActivity from package explorer in Eclipse. Open

Add following code into it:  

package com.example.contextmenusample;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {  
                private static String[] myList = {"MindStick", "Apple", "Microsoft", "Oracle", "Amazon",
                                                                       "Facebook", "WhatsApp"};
                protected void onCreate(Bundle savedInstanceState) {
                                //Creating a list view
                                ListView list = new ListView(this);  
                                //Creating an array adapter
                                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                                //setting adapter to list view
                                list.setAdapter(adapter);                                   //Register a button for context events
                                registerForContextMenu(list);                                   setContentView(list);
                public boolean onCreateOptionsMenu(Menu menu) {
                                // Inflate the menu; this adds items to the action bar if it is present.                                 getMenuInflater().inflate(, menu);                                 return true;
                public void onCreateContextMenu( ContextMenu menu, View v,
                                                                               ContextMenu.ContextMenuInfo menuinfo){                                 super.onCreateContextMenu(menu, v, menuinfo);                                   //inflate the context menu
                                getMenuInflater().inflate(, menu);  
                                //set header name
                                menu.setHeaderTitle("Choose an option");
                public boolean onContextItemSelected(MenuItem item){  
                                //We can obtain the item that was clicked from the
                                // bundled ContextMenuInfo object, which is an instance
                                // of AdapterContextMenuInfo in the case of a ListView
                                AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();                                 int listPosition = info.position;                                   //Switch on the item's ID to find the action the user selected
                                switch (item.getItemId()) {
                                                //perform delete action
                                                //perform edit action
                                                return super.onContextItemSelected(item);
                                return true;  

 Running the application:

Just hit on “Run” button and our app will be launched and our list view consist of company names will be displayed:

 Displaying a Context Menu in Response of User Action

Long press click on any company name, a contextual menu will be appeared on the selected item.

Displaying a Context Menu in Response of User Action


Thanks for reading this post.

Happy Coding!! J


  Modified On Sep-07-2019 12:15:49 AM

Leave Comment