I apologize if this seems really simple but I can’t figure it out. I have an ImageButton that I want to change the image of at run-time based on a button click. I have 5 images in my res folder. When the user hits the “Next” button, I want it to go to the next image. The file names of the images are image1.png, image2.png, etc.
I know I you can change the image by doing:
imgButton.setImageResource(R.drawable.image2);
I have a counter (int) to keep track of the image number being displayed. But how would i change the image to the next image? Any help would be appreciated.
Create an Array of integers to hold the references to your images, e.g.
And then increment your counter on each click, and set the image resource using a value from your array:
Easy when you know how… 😉