Differentiate View accessed by a reused ContextMenu in several Views

3

I'm developing an Android app in which I have two images on a screen, client and your vehicle.

I want to show a menu by giving a long click on both images and so on, by clicking on both imageView the same menu is shown, however, how do I differentiate between them? the onContextItemSelected method?

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 could not 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);
}
    
asked by anonymous 14.12.2014 / 12:23

1 answer

2

In the onCreateContextMenu method, it is passed to view that requested the creation of the menu so, depending on that view , you can initialize a variable that will indicate which image was clicked.

private int imagemClicadaId;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

    imagemClicadaId = v.getId();
    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");
}  

In the onContextItemSelected method, use imagemClicada to act according to what you want to do in each case.

@Override
public boolean onContextItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case MENU_TAKE_PHOTO: {
            if(imageClicadaId == R.id.imagemCliente){
                helper.startCameraForClient();
            }
            else{ 
                helper.startCameraForVehicle();
            }
        }
        case MENU_VIEW_PHOTO: {
            Intent intent = new Intent(getBaseContext(), FotoFullscreen.class);
            if(imageClicadaId == R.id.imagemCliente){
                intent.putExtra("image", PHOTO_CLIENT);
            }
            else{
                intent.putExtra("imagem", PHOTO_VEHICLE);
            }

            startActivity(intent);
        }
        case MENU_DEL_PHOTO: {

        }
    }
    return super.onContextItemSelected(item);
}
    
14.12.2014 / 16:29