Wednesday 29 August 2012

Android Content Menu using XML file and Event Handling- 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:
  • We will override onContextItemSelected() to Listen the Item Event Selected

Context Menu with ItemListener :

  • 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>
    
  • We Create Menu using XML file res/menus/menutxt.xml for TextView as follows.. 
  • <?xml version="1.0" encoding="utf-8"?>
    <menu
      xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/M1"
         android:title="Menu 1" />
        <item android:id="@+id/M2"
         android:title="Menu 2" />
        <item android:id="@+id/M3"
         android:title="Menu 3" />
    </menu>
    
    
  • We Create Menu using XML file res/menus/menucm.xml for TextView as follows..
  • <?xml version="1.0" encoding="utf-8"?>
    <menu
      xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menuStart"
         android:title="Start" />
        <item android:id="@+id/menuStop"
         android:title="Stop" />
        <item android:id="@+id/menuRestart"
         android:title="Restart" />
    </menu>
    
  • Then in the following Activity file in the method onCreateContextMenu() we have created Menu for TextView & Chronometer and Register for each Contrrol and using onContextItemSelected() we will listen the events of Chronometer ..
  • 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 ContextMenuXmlActivity 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
      switch(item.getItemId())
      {
       case R.id.menuStart:
        cm.start();
        break;
       case R.id.menuStop:
        cm.stop();
        break;
       case R.id.menuRestart:
        cm.setBase(android.os.SystemClock.elapsedRealtime());
        break;
      }
      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)
      {
       getMenuInflater().inflate(R.menu.menutxt, menu);
      }
      if(v.getId()==R.id.chronometer1)
      {
       getMenuInflater().inflate(R.menu.menucm, menu);
       menu.setHeaderIcon(android.R.drawable.ic_media_play);
       menu.setHeaderTitle("Timer.....");
      }
      super.onCreateContextMenu(menu, v, menuInfo);
     }
        
    }
  • When you long click on Chronometer the following ContextMenu will be displays. 
  • And you can perform "Start", "Stop" and "Restart"  the Chronometer.
  •  

No comments:

Post a Comment