Context menu in Android with example



                This tutorial explains about creating Context menu in Android. It can be seen in Android main screen, when you long press/click the screen it will get activated. Android context menu is similar to context menu in Windows or Linux, the one which is displayed on right click. Now let’s see an example to create Context menu in Android.

registerForContextMenu(View v)
                First of all we need to register view for using Context menu. This method will set the View.OnCreateContextMenuListener on the view to this activity, so onCreateContextMenu(ContextMenu, View, ContextMenuInfo) will be called when it is time to show the context menu.

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Context Menu");
     menu.add(0, v.getId(), 0, "Action 1");
     menu.add(0, v.getId(), 0, "Action 2");
     menu.add(0, v.getId(), 0, "Action 3");
}

onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
                This method is triggered when the screen is long pressed and context menu view is being built
menu –  The actual context menu being built, using which we can set its properties.

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Context Menu");
    menu.add(0, v.getId(), 0, "Action 1");
    menu.add(0, v.getId(), 0, "Action 2");
    menu.add(0, v.getId(), 0, "Action 3");
}

onContextItemSelected(MenuItem item)
                This is called whenever an item in the context menu is selected and the interface MenuItem provides necessary details on the selected item. Through which we can write our own functionality to be perform based on item selection. 


public boolean onContextItemSelected(MenuItem item) {
   if (item.getTitle() == "Action 1") {
      Toast.makeText(this, "Action 1 invoked", Toast.LENGTH_SHORT).show();
   }
   else if (item.getTitle() == "Action 2") {
      oast.makeText(this, "Action 2 invoked", Toast.LENGTH_SHORT).show();
   }
   else if (item.getTitle() == "Action 3") {
      Toast.makeText(this, "Action 3 invoked", Toast.LENGTH_SHORT).show();
   } 
   else {
      return false;
   }
   return true;
}

On the layout page I have just display a text and button, long press button to see the output shown below.

Context Menu

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to compiletimeerror.com" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Long press me!" />

</LinearLayout>

MainActivity.java

package com.example.contextmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Button btn = (Button) findViewById(R.id.btn);
  registerForContextMenu(btn);
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  menu.setHeaderTitle("Context Menu");
  menu.add(0, v.getId(), 0, "Action 1");
  menu.add(0, v.getId(), 0, "Action 2");
  menu.add(0, v.getId(), 0, "Action 3");
 }
 @Override
 public boolean onContextItemSelected(MenuItem item) {
  if (item.getTitle() == "Action 1") {
   Toast.makeText(this, "Action 1 invoked", Toast.LENGTH_SHORT).show();
  } else if (item.getTitle() == "Action 2") {
   Toast.makeText(this, "Action 2 invoked", Toast.LENGTH_SHORT).show();
  } else if (item.getTitle() == "Action 3") {
   Toast.makeText(this, "Action 3 invoked", Toast.LENGTH_SHORT).show();
  } else {
   return false;
  }
  return true;
 }

}

Have a look at Android options menu here

Download Source code of this application

 



Reactions:

3 comments :