A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.The contextual action mode displays action items that affect the selected content in a bar at the top of the screen and allows the user to select multiple items.
Creating Context Menu
Register the View to which the context menu should be associated by callingregisterForContextMenu() and pass it the View. If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or GridView to registerForContextMenu().
Implement the
onCreateContextMenu() method in your Activity or Fragment.When the registered view receives a long-click event, the system calls your onCreateContextMenu()method. This is where you define the menu items, usually by inflating a menu resource. For example: @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
MenuInflater allows you to inflate the context menu from a menu resource. The callback method parameters include the Viewthat the user selected and a ContextMenu.ContextMenuInfo object that provides additional information about the item selected. If your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate. Implement onContextItemSelected(). When the user selects a menu item, the system calls this method so you can perform the appropriate action. For example:@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
So we learned the basics of context menu.Now let us do it in an example
activity_main
We are going to add a listview in our layout.We need to make context menu visible on long pressing the ListView<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_alignParentLeft="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>MainActivity.java
public class MainActivity extends Activity {
ListView listView1;
String contacts[]={"item1","item2","item3","item4"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contacts);
listView1.setAdapter(adapter);
// Register the ListView for Context menu
registerForContextMenu(listView1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("CONTEXT MENU");
menu.add(0, v.getId(), 0, "one");
menu.add(0, v.getId(), 0, "two");
}
@Override
public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()=="one"){
Toast.makeText(getApplicationContext(),"sucess1",Toast.LENGTH_LONG).show();
}
else if(item.getTitle()=="two"){
Toast.makeText(getApplicationContext(),"sucess2",Toast.LENGTH_LONG).show();
}else{
return false;
}
return true;
}
} 

No comments:
Post a Comment