0

I have three Images. When I first touch, first Image is shown, when I second touch, second Image is shown, and then when I third touch, third Image is shown. After all, when I fourth touch, I want to show first Image return and so on.

Can someone point me to show how handling or touching on a ImageView of android?

2
  • try to describe your problem more effectively. an Image also helps Commented May 16, 2017 at 10:48
  • why don't you use an int counter variable... increase its value +1 on every click, and check the count and show the image.. that's it. any problem with this method ?? Commented May 16, 2017 at 10:59

2 Answers 2

1

The below should do what you need:

public class MainActivity extends Activity { ImageView image; int i=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); image = (ImageView) findViewById(R.id.imageViewName); setButtonOnClickListeners(); } } image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (i==1){ image.setImageResource(R.drawable.image1); i++; } else if(i==2){ image.setImageResource(R.drawable.image2); i++; } else if(i==3){ image.setImageResource(R.drawable.image3); i++; } else if(i==4){ image.setImageResource(R.drawable.image4); i=1; } } }); 

I've not tested but the idea is correct. When you click the image it will change the drawable depending on the value of i. When you get image 1 i will equal 1. Onclick will increment i until i==4 which it will reset to 1 as you requested.

A while loop might be tidier but this was the quickest soultion I thought of.

Sign up to request clarification or add additional context in comments.

Comments

0

how to handle click on an ImageView

You can simply set a View.OnClickListener for your ImageView:

imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do stuff } }); 

3 Comments

I already write setOnClickListener method. How to handle touch count inside this method?
I think @SRBbans had already provide a solution. or maybe I could implement for you.
Yes, Thank You @LionHere

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.