3
I’m developing an application for Android in which I have two images on a screen, client and its vehicle.
I want to show a menu by clicking a long click on both images and so far so good, by clicking on both imageView the same menu is shown, however, as I do to differentiate them in the method onContextItemSelected?
This is the code:
I registered to show the menu in both imageView
@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.tela_cliente_cadastrar); registerForContextMenu(helper.img_clienteClick()); registerForContextMenu(helper.img_veiculoClick());
...
I created the menu
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.setHeaderTitle("Opções"); menu.add(Menu.NONE, MENU_TAKE_PHOTO, 0, "Take photo"); menu.add(Menu.NONE, MENU_VIEW_PHOTO, 0, "View"); menu.add(Menu.NONE, MENU_DEL_PHOTO, 0, "Delete");
}
And here’s the problem because I couldn’t find a way to discern one from the other:
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case MENU_TAKE_PHOTO: {
helper.startCameraForClient();
//or helper.startCameraForVehicle();
}
case MENU_VIEW_PHOTO: {
Intent intent = new Intent(getBaseContext(), FotoFullscreen.class);
intent.putExtra("image", PHOTO_CLIENT);
//OR intent.putExtra("imagem", PHOTO_VEHICLE);
startActivity(intent);
}
case MENU_DEL_PHOTO: {
}
}
return super.onContextItemSelected(item);
}
Peter, the
ContextMenuItemInfo
ofMenuItem
does not return anything useful to differentiate the two Views?– Wakim
There are some ways to create the same menu and reuse it in various Listviews?
– Pedro
@Wakim de facto the
ContextMenuItemInfo
ofMenuItem
has everything necessary to differentiate the twoviews
. Despite my answer solve the problem, it would be useful for you to answer as well, because the use ofContextMenuItemInfo
is more elegant.– ramaral
@ramaral, I’ve been doing some tests here in the meantime. In his case, the
ContextMenuInfo
will always be null. Only if he was using theregisterForContextMenu
in aAdapterView
, theContextMenuInfo
would be a subclass ofAdapterView.AdapterContextMenuInfo
and in thegetMenuInfo
would come an instance that would have the exact information ofView
that was clicked. But for this situation, I think your solution is the most suitable in fact.– Wakim
@Wakim It’s true, it’s been a while since I’ve programmed on Android! Went looking for code I’ve done and already used both methods:
ContextMenuInfo
when it’s aListView
and this, my answer, when it is not.– ramaral