Friday, July 15, 2011

Context Menu Android

Context Menu is similar to right click on your desktop system.

In Android context menu is used with list view. In we long press list view item then the menu event fires.

In order to use Context Menu with the list view we have register the Context Menu for the list view. The following snippet explain the use of Context Menu with the list view.

Example:

main.xml
 <?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">
  
    <ListView
        android:layout_width="fill_parent"
        android:id="@+id/lvContextMenu"
        android:layout_height="fill_parent">
    </ListView>   
       
</LinearLayout>

Home.java
package com.lvcm.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends Activity
{
    ListView lvContextMenu;
    ListAdapter adapter;
    private String[] MenuItems = {"Edit", "Delete"};
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //Getting the List View from the xml file
        lvContextMenu = (ListView)findViewById(R.id.lvContextMenu);
        lvContextMenu.setAdapter(new ListAdapter());
       
        //Registering Context Menu
        registerForContextMenu(lvContextMenu);
    }
   
    // Context Menu Creation
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        if (v.getId()==R.id.lvContextMenu)
        {           
            menu.setHeaderTitle("CONTEXT MENU");
            for (int i = 0; i< MenuItems.length; i++)
            {
                menu.add(Menu.NONE, i, i, MenuItems[i]);
            }
      }
    }
   
   // Context Menu Item Selection
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        AdapterView.AdapterContextMenuInfo info =                                                                            (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      
        // Getting the Id
        int menuItemIndex = item.getItemId();
        Toast.makeText(Home.this, "Clicked Item Position :"+info.position+"\n"+"Seleted Option Id :"+menuItemIndex, Toast.LENGTH_SHORT).show();       
        return true;
    }
     
    //List Adapter
    public class ListAdapter extends BaseAdapter
    {
        @Override
        public int getCount()
        {           
            return 10;
        }

        @Override
        public Object getItem(int arg0)
        {           
            return arg0;
        }

        @Override
        public long getItemId(int arg0)
        {           
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2)
        {
            TextView tv;
           
            if(arg1 == null)
            {
                tv = new TextView(Home.this);
            }
            else
            {
                tv = (TextView) arg1;
            }           
            tv.setText("list item");
            return tv;
        }       
    }
}
OutPut:

2 comments: