Android UI How to - Context menu action handler








The following code shows how to implement Context menu action handler.

Example

Main layout xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">                        
    <TextView android:layout_width="fill_parent" 
          android:layout_height="wrap_content"
            android:id="@+id/testText" 
          android:text="BEFORE KEYSTROKE DETECTED TEXT!" 
          android:textColor="#FFDDAA" 
          android:textSize="19px"/>          
  <Button android:text="Long-Click Here to Access ContextMenu" 
      android:id="@+id/contextButton"
      android:nextFocusUp="@+id/thirdButton"
      android:nextFocusDown="@+id/secondButton"      
      android:layout_gravity="center"      
      android:layout_width="wrap_content"       
      android:layout_height="wrap_content"/>            
  <Button android:text="Second Button" 
      android:id="@+id/secondButton"
      android:nextFocusUp="@+id/contextButton"
      android:nextFocusDown="@+id/thirdButton"            
      android:layout_gravity="center"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/>      
  <Button android:text="Third Button" 
      android:id="@+id/thirdButton"
      android:nextFocusUp="@+id/secondButtonButton"
      android:nextFocusDown="@+id/contextButton"            
      android:layout_gravity="center"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/>      
</LinearLayout>

Main Activity Java code

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
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;
//from  w w  w. j  a  v a  2 s .  co m
public class MainActivity extends Activity {  
    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button secondButton = (Button) findViewById(R.id.secondButton);
        registerForContextMenu(secondButton);
        Button thirdButton = (Button) findViewById(R.id.thirdButton);
        registerForContextMenu(thirdButton);
        Button contextButton = (Button) findViewById(R.id.contextButton);
        registerForContextMenu(contextButton);        
    }
    @Override    
    public void onCreateContextMenu(ContextMenu menu, View view,ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, view, menuInfo);
    menu.setHeaderTitle("Android Context Menu");
    menu.add(0, view.getId(), 0, "Invoke Context Function 1");
    menu.add(0, view.getId(), 0, "Invoke Context Function 2");
  }
    @Override
  public boolean onContextItemSelected(MenuItem item) {
         if(item.getTitle()=="Invoke Context Function 1"){contextFunction1(item.getItemId());}
      else if(item.getTitle()=="Invoke Context Function 2"){contextFunction2(item.getItemId());}
      else {return false;}
  return true;
  }
    public void contextFunction1(int id){
      Toast.makeText(this, "function 1 invoked!", Toast.LENGTH_SHORT).show();
    }
    public void contextFunction2(int id){
      Toast.makeText(this, "function 2 invoked!", Toast.LENGTH_SHORT).show();
    }        
  public boolean onKeyDown(int keyCode, KeyEvent event) {        
    if (keyCode == KeyEvent.KEYCODE_ENTER) {    
          textUpdate();
                return true;
        }
      return false;
    }  
  public void textUpdate() {
    TextView text = (TextView)findViewById(R.id.testText);
    text.setText("ENTER KEY PRESSED!");    
  }
}
null