Last time, we implemented a  very popular UI patterns in Android called Navigation drawer : Navigation Drawer Implementation in Android  Now we learn how to use contextual menus in our application.

Android context menu appears when the user press long click on the element. It is also known as floating menu. 


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 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 explorer in Eclipse.

Open activity_main.xml

Add following code into it:  



 Main Activity Class Implementation: 

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

Add following code into it:

package com.example.msclient010.mindstickcontextmenu;
import android.os.Bundle;
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;
    protected void onCreate(Bundle savedInstanceState) {
        //getting reference of list view
        list = (ListView) findViewById(; 
        //adding company names in array List to display in list view
        companies = new ArrayList<String>();
        //creating an adapter object
        adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,, companies);
        //setting adapter to list view
        //setting on long press click handler on list view items
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {                 //registering for context menu items
                pos = position;
                return false;
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, 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");
    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 == {
            return true;
        return super.onOptionsItemSelected(item);
    public boolean onContextItemSelected(MenuItem item) { 
        //deleting an item from company list and list view
        if (item.getTitle() == "Delete") {
            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:


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


Click on delete button the selected company will be deleted from the list view and a Toast message “Company is deleted” will be appeared at the bottom of the screen


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


  Modified On Nov-28-2017 11:21:58 PM

Leave Comment