For creating context menu in an activity, Create a folded called menu in the res folder, add a xml file in the res/menu folder. you can give any name to the xml file.
Below is the sample xml file for context menu in the res/menu folder.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/take_photo"
android:title="Take New Photo ">
</item>
<item android:id="@+id/choose_gallery"
android:title="Choose from Gallery">
</item>
<item android:id="@+id/share_cancel"
android:title="Cancel">
</item>
</menu>
Add as much of menu item by using the item tag. The item tag contain id for the menu and title for the menu.
For accessing the context menu, First register the context menu for any control. here i registered context menu for a button.
The context menu will be displayed, on clicking the button01 button.
Add the following code in the activity for opening the context menu and perform some action from selected context menu option.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Post Image");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.camer_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.take_photo:
Toast.makeText(context, "Selected Take Photo", Toast.LENGTH_SHORT).show();
break;
case R.id.choose_gallery:
Toast.makeText(context, "Selected Gallery", Toast.LENGTH_SHORT).show();
break;
case R.id.share_cancel:
closeContextMenu();
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
Here i displayed Toast on selecting option from the context menu.
Below is the sample output.
Below is the sample xml file for context menu in the res/menu folder.
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/take_photo"
android:title="Take New Photo ">
</item>
<item android:id="@+id/choose_gallery"
android:title="Choose from Gallery">
</item>
<item android:id="@+id/share_cancel"
android:title="Cancel">
</item>
</menu>
Add as much of menu item by using the item tag. The item tag contain id for the menu and title for the menu.
For accessing the context menu, First register the context menu for any control. here i registered context menu for a button.
Button button01=(Button)findViewById(R.id.button01);
registerForContextMenu(button01);The context menu will be displayed, on clicking the button01 button.
Add the following code in the activity for opening the context menu and perform some action from selected context menu option.
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Post Image");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.camer_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.take_photo:
Toast.makeText(context, "Selected Take Photo", Toast.LENGTH_SHORT).show();
break;
case R.id.choose_gallery:
Toast.makeText(context, "Selected Gallery", Toast.LENGTH_SHORT).show();
break;
case R.id.share_cancel:
closeContextMenu();
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
Here i displayed Toast on selecting option from the context menu.
Below is the sample output.
No comments:
Post a Comment