Context menu
package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class Test extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Register a button for context events
Button button = new Button(this);
button.setText("Click for Options");
button.setOnClickListener(listener);
registerForContextMenu(button);
setContentView(button);
}
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
openContextMenu(v);
}
};
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu, menu);
menu.setHeaderTitle("Choose an Option");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
//Switch on the item? ID to find the action the user selected
switch(item.getItemId()) {
case R.id.menu_delete:
//Perform delete actions
return true;
case R.id.menu_copy:
//Perform copy actions
return true;
case R.id.menu_edit:
//Perform edit actions
return true;
}
return super.onContextItemSelected(item);
}
}
//contextmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_delete"
android:title="Delete Item"
/>
<item
android:id="@+id/menu_copy"
android:title="Copy Item"
/>
<item
android:id="@+id/menu_edit"
android:title="Edit Item"
/>
</menu>
Related examples in the same category