I am using the following method to add a ContextMenu to a custom view i have built but i want to know how to react to the press of that contextmenu.
This is not an Activity so i cannot do this:
@override
public boolean onOptionsItemSelected(MenuItem item) {
Here is the code
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
Update:
Here is a very simplified verion of my class.
public final class NewView extends View {
public NewView(Context context, AttributeSet attrs) {
super(context, attrs);
cntxt = context;
this.setLongClickable(true);
this.setOnLongClickListener(vLong);
this.setOnCreateContextMenuListener(vC);
}
private View.OnLongClickListener vLong = new View.OnLongClickListener() {
public boolean onLongClick(View view) {
showContextMenu();
return true;
}
};
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
}
Use
item.getItemId()and createswitchandcasesbased on the number returned bygetItemId()Something like this.
I hope this is what you meant by reacting on menu items selection. 🙂