Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

gramjoirps
2,048 PointsChange Image Dynamically in Android
Hello Everyone,
I can set an image dynamically in Android but
how about having 6 images and every time I click on the button it should change the image in imageview.
I know how to do it with random class that's something I learned here but how about changing images in order.
I hope somebody helps me out.
3 Answers

Blake Gall
2,517 PointsThe way I'm thinking this would work is by using setImageResource() method and also every time you click the button, it'll add 1 to a counter. once the counter reaches 5, it will reset. <br> Outside of Main() : <br>
public int counter = 0;
<br> Inside of OnClick: <br>
OnClick(View v){
if(counter == 0){
imageView.setImageResource(R.drawable.image1);
}
if(counter == 1){
imageView.setImageResource(R.drawable.image2);
}
if(counter == 2){
imageView.setImageResource(R.drawable.image3);
}
if(counter == 3){
imageView.setImageResource(R.drawable.image4);
}
if(counter == 4){
imageView.setImageResource(R.drawable.image5);
}
if(counter == 5){
imageView.setImageResource(R.drawable.image6);
counter = -1;
}
counter++
}
<br> I tried something close to this for a crystal ball prank app but it didn't work the way I thought. Please tell me if this works for you.
Edit: you might want to use an else statement after that last if statement but do not put the counter++ in that else statement

Steve Hunter
57,712 PointsHi,
That looks OK - would a switch/case
be less wordy? You might also consider calling a separate function inside onClick
so that the code is kept in its own place.
It'd be great to use an array of images so you could just use the index, R.drawable.imageArray[counter]
. That'd be far less code! But I don't know if that's doable.

gramjoirps
2,048 PointsCan you please show the array way step by step please?

Steve Hunter
57,712 PointsI don't think the array would work - let me try it first! I need to create a project to start with - I've just rebuilt the Mac so all my previous projects have gone.

gramjoirps
2,048 PointsYour help will be much appreciated.
gramjoirps
2,048 Pointsgramjoirps
2,048 PointsThank you for replying but it crashes the app and doesn't work.
Blake Gall
2,517 PointsBlake Gall
2,517 PointsThats the only way I could think of it. If there are other answers I knew I would have used them
gramjoirps
2,048 Pointsgramjoirps
2,048 PointsHey man, thanks it worked.
Just wondering what do you mean by last one counter = -1;
It worked without that.
Blake Gall
2,517 PointsBlake Gall
2,517 PointsSince that counter++ would run anyways, i wanted to set the integer to -1 so when the counter++ ran, counter would reset to 0