Monday, June 18, 2012

How To Implement Context Menu Into Your Application

The project describes about how you can implement context menu into your application. Context menu is a floating list of menu items that appears when the user touches and holds a view that’s registered to provide a context menu.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project MyMenu
2.) Open the main.xml file and insert the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        <ListView android:id="@+id/list" android:layout_width="fill_parent"
                android:layout_height="0px" android:layout_weight="1" />
        <TextView android:id="@+id/footer" android:layout_width="fill_parent"

                android:layout_height="60dip" android:text="@string/footer"
                android:padding="4dip" android:background="#FF666666" />
Along with this create and open res/layout/listitem.xml and insert following:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:textSize="24dip" android:padding="8dip" />
3.) Open and insert the following strings in strings.xml:
    <string name="footer">Click and hold on to one of the items above to show the context menu.</string>
    <string name="app_name">MyMenu</string>
        <string-array name="Game">
        <item>New Game</item>
        <string-array name="contextmenu">
4.) Make sure you write following method in MyMenu class:
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
        if (v.getId()
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)menuInfo;
String[] menuItems = getResources().getStringArray(R.array.contextmenu);
                for (int i = 0; i<menuItems.length; i++)
                        menu.add(Menu.NONE, i, i, menuItems[i]);
The “AdapterView.AdapterContextMenuInfo info (AdapterView.AdapterContextMenuInfo)menuInfo;” gives you more details about the list item clicked. Then you can use, info.position and so on to retrieve the details and use those actions (edit, delete…).
5.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyMenu
2.) Then enter the following information:
Project name: MyMenu
Build Target: Android 1.6
Application name: MyMenu
Package name: org.example.MyMenu
Create Activity: MyMenu
On Clicking Finish MyMenu code structure is generated with the necessary Android Packages being imported along with Following code must be added in MyMenu class to get the menu working.
public void onCreate(Bundle savedInstanceState)
        Countries = getResources().getStringArray(R.array.Game);
        ListView list = (ListView)findViewById(;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, Countries);
public boolean onContextItemSelected(MenuItem item)
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int menuItemIndex = item.getItemId();
        String[] menuItems = getResources().getStringArray(R.array.contextmenu);
        String menuItemName = menuItems[menuItemIndex];
        String listItemName = Countries[info.position];
        TextView text = (TextView)findViewById(;
text.setText(String.format("Selected %s for item %s", menuItemName, listItemName));
        return true;
Output –The final output:

No comments:

Post a Comment