Android Context Menu Example

By | February 14, 2013
Android context is a floating menu which displays a list of menu items on long click of a view,which has a menu associated with it

Lets see an example of how to create context

Project Detail

Project Name Context_Menu_Demo
Package com.pavan.context_menu
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

file:-activity_main.xml

<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:id=”@+id/layout_id”>

    <Button
        android:id=”@+id/menu_but”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignParentTop=”true”
        android:layout_centerHorizontal=”true”
        android:layout_marginTop=”94dp”
        android:text=”Menu” />

</RelativeLayout>

2. XML Layout For Menu


Create menu.xml file in res/menu and inside menu.xml file each menu item is assoicated with  id which uniquely identifies each menu item and  title to display it’s name

file:- menu.xml

<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
      <item android:id=”@+id/red” android:title=”RED”></item>
      <item android:id=”@+id/green” android:title=”GREEN”></item>
      <item android:id=”@+id/blue” android:title=”BLUE”></item>
      <item android:id=”@+id/gray” android:title=”GRAY”></item>
      <item android:id=”@+id/cyan” android:title=”CYAN”></item>
     <item android:id=”@+id/yellow” android:title=”YELLOW”></item>
</menu>

3. Activity

Create a MainActivity.class and extend it to activity class and override the onCreate() method of activity class and set the content of the actvity with the above defined xml file by call  setContentView() method and passing xml file name as a parameter to it

                               setContentView(activity_main);
       
     and get the id of the button view declared in activty_main.xml , and then we need to register this button view for context menu by calling

                                registerForContextMenu(m_button);

  
file: – MainActivity.java 

public class MainActivity extends Activity {

 Button m_button;
 RelativeLayout ourlayout;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
   m_button = (Button) findViewById(R.id.menu_but);
  registerForContextMenu(m_button);
  }

3.1 Inflating menu

Now to make a menu.xml file to float up on long click of button view , so we need override the onCreateContextMenu() of activity class and inside this method we make a call to getMenuInflater().inflate(R.menu.menu, menu) and passing  the menu.xml file name and contextmenu object as a parameter to inflate it  

you can also set the title for context menu by calling setHeaderTitle on contextmenu object          
                             menu.setHeaderTitle(“Context Menu”);

public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  // TODO Auto-generated method stub
  super.onCreateContextMenu(menu, v, menuInfo);
  getMenuInflater().inflate(R.menu.menu, menu);
     menu.setHeaderTitle(“Context Menu”);
 }

3.2 Responding menu item click


Now to respond to menu item click we override the onContextItemSelected(MenuItem item) of activity class , inside this method we get to know which item is clicked by user by calling  getItemId() on menuitem object .
 

finally the class file would look like this


file: – MainActivity.java

package com.pavan.context_menu;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

 Button m_button;
 RelativeLayout ourlayout;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ourlayout = (RelativeLayout) findViewById(R.id.layout_id);
  m_button = (Button) findViewById(R.id.menu_but);
  registerForContextMenu(m_button);
 }

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {

  super.onCreateContextMenu(menu, v, menuInfo);
  getMenuInflater().inflate(R.menu.menu, menu);
     menu.setHeaderTitle(“Context Menu”);
 }

 @Override
 public boolean onContextItemSelected(MenuItem item) {
 
      switch (item.getItemId()) {
         case R.id.red:
                   ourlayout.setBackgroundColor(Color.RED);
                   break;

         case R.id.green:
                ourlayout.setBackgroundColor(Color.GREEN);
                break;

         case R.id.blue:
                 ourlayout.setBackgroundColor(Color.BLUE);
                break;

          case R.id.gray:
              ourlayout.setBackgroundColor(Color.GRAY);
              break;
   
         case R.id.cyan:
             ourlayout.setBackgroundColor(Color.CYAN);
             break;

        case R.id.yellow:
             ourlayout.setBackgroundColor(Color.YELLOW);
             break;

  }
  return super.onOptionsItemSelected(item);
 }
}

4. RUN

Android Context Menu
More on Menus

  1. Android Options Menu
  2. Android Popup Menu
  3. Android Sliding Menu