I have several buttons registered for the context menu
How to find out which button was pressed to display the menu?
below is the pseudo code that I will use. I need to do something related to the button click (I have a few more buttons for the announcement), from where I find out that the context menu is activated from which the button was clicked.
EDITOR: I think I didn’t understand, I wanted to know which button was pressed so that the menu appeared. No menu item selected. In any case, I have a solution that I will add in the near future.
thank
private static final int SEND_AS_TEXT = Menu.FIRST;
private static final int SEND_AS_IMAGE = Menu.FIRST + 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendAllBtn = (Button)findViewById(R.id.sendAllBtn);
sendAllBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case SEND_AS_TEXT:
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
}
source
share