---
title: "Contextual Menu Implementation in Android"  
description: "Hie Everyone, here we are going to learn how to create contextual menus in andriod"  
author: "Anonymous User"  
published: 2015-02-04  
updated: 2019-10-29  
canonical: https://www.mindstick.com/articles/1568/contextual-menu-implementation-in-android  
category: "android"  
tags: ["android", "android activity", "android layout", "android listview"]  
reading_time: 5 minutes  

---

# Contextual Menu Implementation in Android

Last time, we implemented a very popular UI patterns in [Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) called [Navigation](https://www.mindstick.com/blog/62/different-navigation-in-asp-dot-net) drawer : Navigation Drawer [Implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) in Android Now we learn how to use contextual menus in our [application](https://www.mindstick.com/articles/12824/calculator-application-in-android).\

Android context menu appears when the user press long click on the element. It is also known as [floating](https://www.mindstick.com/articles/85595/how-to-troubleshoot-when-your-floating-pond-fountain-is-not-working-properly) menu.

##### 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 “MindStickContextMenu”.

Creating Layouts and views for our GUI:

Now, we need to add views in our view groups, i.e. add widgets to our GUI.

We need

1. A [Relative](https://answers.mindstick.com/qa/105067/how-to-trade-with-the-relative-vigor-index) or Linear Layout consists of list view to hold the items to be shown

in list.

For this, navigate to res/Layout/activity_main.xml 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 activity_main.xml

Add following code into it:

```
<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity"> 
    <ListView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/list"></ListView> 
</RelativeLayout>
```

##### Main Activity Class Implementation:

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

Add following code into it:

```
package com.example.msclient010.mindstickcontextmenu;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import java.util.ArrayList;import java.util.List; 
public class MainActivity extends ActionBarActivity {
    // list view  to display list items    ListView list;    //list of companies consist of item to be contained in list view    List<String> companies;    // position of item in the list    int pos;
    //Array adapter to hold the Array list in the list view    ArrayAdapter adapter; 
    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
        //getting reference of list view        list = (ListView) findViewById(R.id.list); 
        //adding company names in array List to display in list view        companies = new ArrayList<String>();        companies.add("MindStick");        companies.add("Google");        companies.add("Amazon");        companies.add("Apple");        companies.add("Microsoft");        companies.add("Oracle"); 
        //creating an adapter object        adapter = new ArrayAdapter(this,                android.R.layout.simple_list_item_1, android.R.id.text1, companies);
        //setting adapter to list view        list.setAdapter(adapter); 
        //setting on long press click handler on list view items        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {            @Override            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //registering for context menu items                registerForContextMenu(list);                pos = position;                return false;            }
        }); 
    } 
    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return false;
    }
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //setting header for menu        menu.setHeaderTitle("Select An Action");
        //setting menu options        menu.add(0, v.getId(), 0, "Delete");//groupId, itemId, order, title        menu.add(0, v.getId(), 0, "Update");        menu.add(0, v.getId(), 0, "Cancel");
    } 
    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId(); 
        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {
            return true;
        } 
        return super.onOptionsItemSelected(item);    }

    @Override    public boolean onContextItemSelected(MenuItem item) { 
        //deleting an item from company list and list view        if (item.getTitle() == "Delete") {            companies.remove(pos);            adapter.notifyDataSetChanged();            Toast.makeText(getApplicationContext(), "Company is deleted", Toast.LENGTH_LONG).show();
            pos = 0;            return true;        } else if (item.getTitle() == "Update") {            Toast.makeText(getApplicationContext(), "Please update company", Toast.LENGTH_LONG).show();
        } else {            return false;        }
        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:

![Contextual Menu Implementation in Android](https://www.mindstick.com/mindstickarticle/e8a14283-9d64-4310-88c5-db72744a1f7c/images/87c68919-4af4-480a-ad1a-af8b6b2c8cbf.png)

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

![Contextual Menu Implementation in Android](https://www.mindstick.com/mindstickarticle/e8a14283-9d64-4310-88c5-db72744a1f7c/images/aff690ba-9a85-4238-8751-b498f2725299.png)

Click on delete button the selected company will be [deleted](https://www.mindstick.com/forum/160487/how-to-remove-the-deleted-git-code-marker-in-scrollbar-in-visual-studio-2022) from the list view and a Toast message “Company is deleted” will be appeared at the bottom of the screen

![Contextual Menu Implementation in Android](https://www.mindstick.com/mindstickarticle/e8a14283-9d64-4310-88c5-db72744a1f7c/images/cca2abda-8ef7-44cc-82d4-fa5b29ced0f5.png)

Next we are going to see some Intent concepts in Android : Start a new activity within an Activity in Android

Thanks for reading this post.

Happy Coding!! J

---

Original Source: https://www.mindstick.com/articles/1568/contextual-menu-implementation-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
