Differentiate View accessed by a reused Contextmenu in multiple Views

Asked

Viewed 63 times

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 of MenuItem does not return anything useful to differentiate the two Views?

  • There are some ways to create the same menu and reuse it in various Listviews?

  • @Wakim de facto the ContextMenuItemInfo of MenuItem has everything necessary to differentiate the two views. Despite my answer solve the problem, it would be useful for you to answer as well, because the use of ContextMenuItemInfo is more elegant.

  • @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 the registerForContextMenu in a AdapterView, the ContextMenuInfo would be a subclass of AdapterView.AdapterContextMenuInfo and in the getMenuInfo would come an instance that would have the exact information of View that was clicked. But for this situation, I think your solution is the most suitable in fact.

  • @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 a ListView and this, my answer, when it is not.

1 answer

2


In the method onCreateContextMenu is passed to view that requested the creation of the menu thus, 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 method onContextItemSelected utilize imagemClicada to act in accordance with 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);
}
  • That’s what I needed, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.