How to know which image was clicked in java

Asked

Viewed 321 times

1

I have 4 images in my layout, how to know which one was clicked and do conditional? my scribe:

Oncreate:

ImageView bancada_tv = (ImageView)findViewById(R.id.main_bancada);
ImageView aguarda_tv = (ImageView)findViewById(R.id.main_aguarda);
ImageView pronto_tv = (ImageView)findViewById(R.id.main_pronto);
ImageView entregue_tv = (ImageView)findViewById(R.id.main_entregue);

bancada_tv.setOnClickListener(this);
aguarda_tv.setOnClickListener(this);
pronto_tv.setOnClickListener(this);
entregue_tv.setOnClickListener(this);

onClick:

@Override

public void onClick(View v) {

if(não sei como fazer){
}

}
  • I would recommend using the id of View as a differentiator, but there is the possibility to use Listeners anonymous.

  • I found a way I don’t know if the right one but it helped me. I used the v.getContentDescription(); and in the xml in each image I assigned a android:contentDescription=""

1 answer

3


Just do it like this:

@Override
public void onClick(View v) {
    if( v.getId() == R.id.main_bancada ) {
        // Faça algo para essa View;
    } else if( v.getId() == R.id.main_aguarda ) {
        // Faça algo para essa outra View;
    }

    // Ou então ao invés de usar if, utilize switch case
    switch( v.getId() ) {
        case R.id.main_bancada:
            // Chama algum método;
            break;
        case R.id.main_aguarda:
            // Chama algum método;
            break;
    }
}

Or use anonymous listeners with @Walkim spoke, or set different listeners for each of them. You know the way it will do.

Browser other questions tagged

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