Step-by-Step Tutorial: Creating Context Menus in Android

Android context menu :

Context menu in android is a type used to open a android popup menu when a Long Click is made on list-view. A menu pop’s up giving options like share, delete, edit,etc,.

These menu’s you will find in every app mostly where you can make any configuration changes to the particular field, the options available are then visible accordingly.

Generally used with listview to populate further more options which cannot be displayed there on the list.This types of list menu are much flexible in using and also gives a comfort in design purpose too.

Android context menu saves a lot of space and reuses the same code through-out the app also you can customized the items to be displayed in each screen if required.

In this tutorial i will be showing you android context menu example based on the android context menu with five different options.

Inserting values into String [ ] and showing these values in list-view, and by long click on this values you can get a options menu.

 

MainActivity.java :

In this context menu i am adding five options like share, view, update, delete, delete all you may add and customize the list accordingly.

menu.add(0, 0, 0, "Share"); 
menu.add(0, 1, 1, "View");
menu.add(0, 2, 2, "Update"); 
menu.add(0, 3, 3, "Delete"); 
menu.add(0, 3, 3, "Delete All");

 

Declaring a String array with dummy values as

String[] value = new String[] { "one", "two", "three", "four", "five" };

 

And passing the above values on to the list adapter and then registering our context menu on the listview as

this.registerForContextMenu(listView);

 

After a long press on the particular item and selecting the option we can return selected value and process it.

return super.onContextItemSelected(item);

 

public class MainActivity extends Activity {

	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ListView) findViewById(R.id.listView);

		String[] value = new String[] { "one", "two", "three",
				"four", "five" };

		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1,
				android.R.id.text1, value);

		// Assign adapter to ListView
		listView.setAdapter(adapter);

		this.registerForContextMenu(listView);


	}

	 @Override
	 	public void onCreateContextMenu(ContextMenu menu, View v,
	 			ContextMenuInfo menuInfo) {
	 		// TODO Auto-generated method stub

	 		super.onCreateContextMenu(menu, v, menuInfo);

	 		menu.add(0, 0, 0, "Share");
	 		menu.add(0, 1, 1, "View");
	 		menu.add(0, 2, 2, "Update");
	 		menu.add(0, 3, 3, "Delete");
	 		menu.add(0, 3, 3, "Delete All");

	 	}

	 	@Override
	 	public boolean onContextItemSelected(MenuItem item) {

	 		AdapterView.AdapterContextMenuInfo info
	 		        = (AdapterView.AdapterContextMenuInfo) item
	 				.getMenuInfo();
	 		int itemPosition = info.position;

	 		Uri uri;
	 		int selected = 0;
	 		switch (item.getItemId()) {

	 		case 0:

	 			Toast.makeText(MainActivity.this,
	 			       "Share", Toast.LENGTH_LONG).show();

	 			break;

	 		case 1:

	 			Toast.makeText(MainActivity.this,
	 					"View", Toast.LENGTH_LONG).show();

	 			break;

	 		case 2:

	 			Toast.makeText(MainActivity.this,
	 					"Update", Toast.LENGTH_LONG).show();

	 			break;

	 		case 3:

	 			Toast.makeText(MainActivity.this,
	 					"Delete", Toast.LENGTH_LONG).show();

	 			break;

	 		case 4:

	 			Toast.makeText(MainActivity.this,
	 					"Delete All", Toast.LENGTH_LONG).show();

	 		}
	 		return super.onContextItemSelected(item);

	}


Adding Listview and text view to the xml file.You can also customize the listview with icons and images based on your android context menu requirement.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidcoding.abhi.context_menu.MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_gravity="center"
        android:text="Context-Menu"
        android:layout_marginBottom="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>



AndroidManifest.xml :

There is nothing to be added here in android manifest file this is the basic configuration setup for every app.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidcoding.abhi.context_menu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidcoding.abhi.context_menu.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android context menu output:

This screen below depicts the usage of context menu implementation.

android context menu

 

If you have any query’s in this tutorial on android context menu let me know through comments below.

Like and share this tutorial if you like to get more interesting content from us.

Show Buttons
Hide Buttons
Read previous post:
Enhancing User Experience with Android Splash Screen

  Splash Screen may be a image or sometimes a video depending upon the app. Android Splash Screen is a...

Close