---
title: "Displaying a Context Menu in Response of User Action"  
description: "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, part"  
author: "Anonymous User"  
published: 2015-09-11  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1864/displaying-a-context-menu-in-response-of-user-action  
category: "android"  
tags: ["android", "android activity", "android layout", "android listview", "android controls"]  
reading_time: 7 minutes  

---

# Displaying a Context Menu in Response of User Action

[Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) [context](https://www.mindstick.com/forum/23245/what-is-context-in-android) menu appears when the user press long click on the element. It is also [known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) [floating](https://www.mindstick.com/articles/85595/how-to-troubleshoot-when-your-floating-pond-fountain-is-not-working-properly) menu. Context Menu is a useful solution, particularly when we want to provide a list of [actions](https://www.mindstick.com/forum/156060/what-are-the-actions-performed-by-sql-server) based on an item click in a [ListView](https://www.mindstick.com/articles/12744/listview-in-android) or other AdapterView.\

##### Pre-Requisites:

##### \
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

##### \
contextmenu.xml

Add following code into it:

```
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_delete"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="Delete Item"/>
    <item
        android:id="@+id/menu_edit"
        android:icon="@android:drawable/ic_menu_edit"
        android:title="Edit Item"/>
</menu> 
```

##### Main Activity Class Implementation:

Now, navigate to res/src/MainActivity from [package](https://www.mindstick.com/blog/12619/tips-for-finding-the-right-package-when-you-buy-instagram-followers) [explorer](https://yourviews.mindstick.com/view/81617/now-it-is-time-to-say-goodbye-to-internet-explorer) in Eclipse. Open MAinActivity.java

Add following code into it:

```
package com.example.contextmenusample;import android.app.Activity;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"};
                @Override                protected void onCreate(Bundle savedInstanceState) {                                super.onCreate(savedInstanceState);                                setContentView(R.layout.activity_main);                   
                                //Creating a list view                                ListView list = new ListView(this);                       
                                //Creating an array adapter                                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                                                                                                 android.R.layout.simple_list_item_1,myList);
                                                    //setting adapter to list view                                list.setAdapter(adapter);                    
                                //Register a button for context events                                registerForContextMenu(list);                     
                                setContentView(list);                }                @Override                public boolean onCreateOptionsMenu(Menu menu) {                                // Inflate the menu; this adds items to the action bar if it is present.
                                getMenuInflater().inflate(R.menu.main, menu);
                                return true;
                }        
                @Override                public void onCreateContextMenu( ContextMenu menu, View v,                                                                               ContextMenu.ContextMenuInfo menuinfo){
                                super.onCreateContextMenu(menu, v, menuinfo);                 

                                //inflate the context menu                                getMenuInflater().inflate(R.menu.contextmenu, menu);                        
                                //set header name                                menu.setHeaderTitle("Choose an option");
                }                          @Override                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()) {                                case R.id.menu_delete:
                                                //perform delete action                                                break;                                case R.id.menu_edit:                                                //perform edit action                                default:                                                return super.onContextItemSelected(item);
                                }                                return true;                     
                }} 
```

Running the [application](https://www.mindstick.com/articles/12824/calculator-application-in-android):

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](https://www.mindstick.com/mindstickarticle/254f01d5-ede3-4bf4-93d4-4a98156ad63f/images/87b9fafe-3f7c-4627-90b0-6e95a82bca54.png)

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](https://www.mindstick.com/mindstickarticle/254f01d5-ede3-4bf4-93d4-4a98156ad63f/images/2068e6f9-bcc9-4410-9db8-971b4095f95f.png)

Thanks for [reading](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) this post.

Happy Coding!! J

---

Original Source: https://www.mindstick.com/articles/1864/displaying-a-context-menu-in-response-of-user-action

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
