Header
Group Services: Technology Consulting
phone +91-9999-283-283/9540-283-283
email info@sisoft.in

Context Menu Demo:

Application Description

In this program we try to create a context menu of four items. We create a ListView and added menu items with help of array adapter. Then we inflate menus with the help of menu inflator. This menu was set upon long press of list item. We show toast notification for selection of each menu item in it.

Concept used:

1.List View:

List View is used to display list of names. We place this in layout and set items of list through code in MainActivity.java.

2.Array Adapter:

Array Adapter is used to set array of names in List View. We grab names from menu and set this adapter on list view placed in layout file.

3.Menu Inflator:

We used menu inflator to inflate menu on long press of list item. This menu contains edit, copy, paste and delete menu.

4.Toast Notification:

Toast Notification is used to notify the selection of any item of menu.

 

 

Source Code:

1. MainActivity.java

package com.example.contextmenuexample;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.ListActivity;

import android.content.Intent;

import android.view.ContextMenu;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.view.ContextMenu.ContextMenuInfo;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

 

 

public class MainActivity extends Activity {

 

ListView l_view;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

l_view = (ListView)findViewById(R.id.listView1);

ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,getResources().
getStringArray(R.array.names));

l_view.setAdapter(ad);

//setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.names)));

registerForContextMenu(l_view);

}

@Override

public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

// TODO Auto-generated method stub

super.onCreateContextMenu(menu, v, menuInfo);

MenuInflater inflator = getMenuInflater();

inflator.inflate(R.menu.main_menu, menu);

}

@Override

public boolean onContextItemSelected(MenuItem item) {

// TODO Auto-generated method stub

switch(item.getItemId())

{

case R.id.edit:

Toast.makeText(this, "You have choosen the edit option.", Toast.LENGTH_LONG).show();

return true;

case R.id.copy:

Toast.makeText(this, "You have choosen the copy option.", Toast.LENGTH_LONG).show();

return true;

case R.id.paste:

Toast.makeText(this, "You have choosen the paste option.", Toast.LENGTH_LONG).show();

return true;

case R.id.delete:

Toast.makeText(this, "You have choosen the delete option.", Toast.LENGTH_LONG).show();

return true;

}

return super.onContextItemSelected(item);

}

}

 

2.activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

 

 

<ListView

android:id="@+id/listView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView1"

android:layout_below="@+id/textView1" >

 

</ListView>

 

</RelativeLayout>

 

3.values/string.xml:

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

<string name="app_name">ContextMenuExample</string>

<string name="action_settings">Settings</string>

<string name="edit">Edit</string>

<string name="Copy">Copy</string>

<string name="paste">Paste</string>

<string name="Delete">Delete</string>

<string name="hello_world">Hello world!</string>

<string-array name="names">

<item>Manoj Kumar</item>

<item>Viplav</item>

<item>Samir</item>

<item>Abhishek</item>

<item>Amit</item>

<item>Prabhat</item>

</string-array>

 

</resources>

 

4.menu/main_menu.xml:

 

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

 

<item

android:id="@+id/edit"

android:title="@string/edit"/>

<item

android:id="@+id/copy"

android:title="@string/Copy"/>

<item

android:id="@+id/paste"

android:title="@string/paste"/>

<item

android:id="@+id/delete"

android:title="@string/Delete"/>

 

</menu>

Screen Shots:

About the Contributor...

Manoj Mimanshak

   Trainee at Sisoft Technologies Indirapuram Ghaziabad