Reputation: 3450
I need to do a image changer when I click on the screen.
With this:
image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
image.setImageResource(R.drawable.image_1);
return false;
}
});
I have another images, 5 or 6, and I want to change to another one when I click the image with the code above. How I can do this, when I click screen the imageview change to the next?
I could do this if I had a counter, but I think that this is not the best solution.
Upvotes: 0
Views: 1005
Reputation: 7306
Use this code:
int clicked = 0 ;
images.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (MotionEventCompat.getActionMasked(motionEvent)) {
case MotionEvent.ACTION_UP:
clicked++;
break;
case MotionEvent.ACTION_DOWN:
if(clicked == 1){
images.setImageResource(R.drawable.images_1);
}else if(clicked == 2){
images.setImageResource(R.drawable.images_2);
}else if(clicked == 3){
images.setImageResource(R.drawable.images_3);
}else if(clicked == 4){
images.setImageResource(R.drawable.images_4);
}else if(clicked == 5){
dialog.dismiss();
}
return true;
break;
}
}
}
Upvotes: 1
Reputation: 3450
I am doing the next, but I don't know If there is another posibility:
images.setOnTouchListener(new View.OnTouchListener() {
int clicked = 1;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(clicked == 1){
images.setImageResource(R.drawable.images_1);
}else if(clicked == 2){
images.setImageResource(R.drawable.images_2);
}else if(clicked == 3){
images.setImageResource(R.drawable.images_3);
}else if(clicked == 4){
images.setImageResource(R.drawable.images_4);
}else if(clicked == 5){
dialog.dismiss();
}
clicked++;
return false;
}
});
Upvotes: 0