i am new to android and working on an app with an image button. So what i am trying to do is that i have 7 images named as im_1, im_2 upto im_7 which is stored in drawable folder and on each click the next image should display on the button. eg. on first click the image on the button should be is "im_1" and on the next click "im_2" and so on until "im_7" is reached. On reaching im_7 the button should display the first image again. help me out here please...
Asked
Active
Viewed 385 times
-1
-
duplicate: http://stackoverflow.com/questions/17129589/how-do-i-programmatically-change-the-imagebutton-src-target-when-a-condition-is – mfruizs Sep 30 '16 at 11:53
1 Answers
1
You can try this way
private ImageButton ivSliderButton;
private int currentImage = 1;
ivSliderButton = (ImageButton) findViewById(R.id.iv_imagebutton_slider);
ivSliderButton.setImageResource(R.drawable.icon1);
currentImage++;
ivSliderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (currentImage) {
case 1:
currentImage++;
ivSliderButton.setImageResource(R.drawable.icon1);
break;
case 2:
currentImage++;
ivSliderButton.setImageResource(R.drawable.icon2);
break;
case 3:
currentImage++;
ivSliderButton.setImageResource(R.drawable.icon3);
break;
case 4:
currentImage = 1;
ivSliderButton.setImageResource(R.drawable.icon4);
break;
}
}
});

Sanjay Hirani
- 406
- 2
- 6
- 18