Tuesday 28 August 2012

Android Context Menu - Example

Context Menu :

  • The ContextMenu is a subtype of Menu that you can configure to display when a long press is performed on a View. As the name implies, the ContextMenu provides for contextual menus to display to the user for performing additional actions on selected items. 
  • You need to implement the onCreateContextMenu() method of your Activity for one to display. However, before that is called, you must call the registerForContextMenu() method and pass in the View for which you want to have a context menu. This means each View on your screen can have a different context menu, which is appropriate as the menus are designed to be highly contextual. 
  • Here we have an example of a Chronometer timer, which responds to a long click with
    a context menu:
    Download Android Context Menu :Example Code
  • We have put two Control "TextView" and "Chronometer" in following main.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:text="@string/hello"
        android:id="@+id/TV01"
        />
    <Chronometer android:text="Chronometer" 
     android:id="@+id/chronometer1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />
    </LinearLayout>
    
  • Then in the following Activity file in the method onCreateContextMenu() we have created Menu for TextView & Chronometer and Register for each Contrrol..
  • package com.widget;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Chronometer;
    import android.widget.TextView;
    
    public class ContextMenuActivity extends Activity {
        /** Called when the activity is first created. */
        TextView tv;
        Chronometer cm;
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            cm=(Chronometer)findViewById(R.id.chronometer1);
            tv=(TextView)findViewById(R.id.TV01);
            registerForContextMenu(cm);
            registerForContextMenu(tv);
            
        }
    
     @Override
     public boolean onContextItemSelected(MenuItem item) {
      // TODO Auto-generated method stub
      // Not any Task that will do with XML
      return super.onContextItemSelected(item);
     }
    
     @Override
     public void onCreateContextMenu(ContextMenu menu, View v,
       ContextMenuInfo menuInfo) {
      // TODO Auto-generated method stub
      if(v.getId()==R.id.TV01)
      {
       menu.add("Menu 1");
       menu.add("Menu 2");
       menu.add("Menu 3");
      }
      if(v.getId()==R.id.chronometer1)
      {
       menu.add("Start");
       menu.add("Stop");
       menu.add("Restart");
       menu.setHeaderIcon(android.R.drawable.ic_media_play);
       menu.setHeaderTitle("Timer....");
      }
      
      super.onCreateContextMenu(menu, v, menuInfo);
     }
        
    }
    
  • When you click on TextView & Chronotmeter that will simply display following Menu respectively. not perform any activity

4 comments: