Home > DeveloperSection > Beginner > Displaying a Context Menu in Response of User Action

Displaying a Context Menu in Response of User Action


Android Android  Android Activity  Android Layout  Android Listview  Android Controls 
Ratings:
0 Comment(s)
 1376  View(s)
Rate this:

Displaying a Context Menu in Response of User Action

 

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.

 

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 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:

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.

 

Thanks for reading this post.

Happy Coding!! J

 


Don't want to miss updates? Please click the below button!

Follow MindStick