Stevian
Stevian

Reputation: 143

How to set ImageView Looping while i only have limited images ? (Android)

I have array drawable, the array drawable only have 4 images, and i have could be more than 4 ImageView, How to set image on the ImageView by looping, and when the looping reach more than 4, i want it's return to the first image, so it will be like this:

and so on..

How to do that ? Any help will be appreciate, thank you!

Upvotes: 0

Views: 441

Answers (1)

Niakros
Niakros

Reputation: 266

u can use % operator. It returns remainder of a division. For example, if you have 6 views and 4 images, you can do this way:

ImageView[] viewsArray = new ImageView[6];
//init your array with findViewById()

for(int i = 0; i < viewsArray.length; ++i) {
   viewsArray[i].setImageDrawable(imagesArray[i % 4]); // if your index is 5 % operator will return 1.  
}    

Upvotes: 3

Related Questions