0

I have a imageView, button inside an activity, 10 pictures that are named from stage1 to stage9.

I need you help to solve some problem

I would like to use a button click to change picture that is on imageView to the next one.

I mean, I want to press on the button, and image view shows stage2 image, I press the button again and stage3 picture will be shown.

I have done this using counter var which count number of clicks and then running if statment to see which picture should go next, but it is too long and it is not possible to have more or less pictures.

I would like to know is there a way to do this, and if possible please show me how.

Thanks

code

private void changeImage(int counter) {
    if (counter == 1) {
        image.setImageResource(R.drawable.stage2);
    } else if (counter == 2) {
        image.setImageResource(R.drawable.stage3);
    } else if (counter == 3) {
        image.setImageResource(R.drawable.stage4);
    } else if (counter == 4) {
        image.setImageResource(R.drawable.stage5);
    } else if (counter == 5) {
        image.setImageResource(R.drawable.stage6);
    } else if (counter == 6) {
        image.setImageResource(R.drawable.stage7);
    } else if (counter == 7) {
        image.setImageResource(R.drawable.stage8);
    } else if (counter == 8) {
        image.setImageResource(R.drawable.stage9);
    }       
}

bassically this is the code that I am using right now.

It works, but if I want to do it to be more dynamic.

depecheSoul
  • 896
  • 1
  • 12
  • 29

2 Answers2

1

Just get resource id by name, use like,

private void changeImage(int counter) {

     if(counter >= 1 && counter <= 9) // Always check counter value before accessing as resource id
     {
      int counterValue = counter+1;  
      int resourceId = Resources.getSystem().getIdentifier("stage"+counterValue, "drawable",  this.getPackageName()); // Use application context to get package name
      image.setImageResource(resourceId);
     }
} 
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Thanks for answer. I tried the code but image is not shown. – depecheSoul Jun 23 '15 at 14:43
  • @depecheSoul - Have you tried `getPackageName()` ? I updated my answer. – user370305 Jun 23 '15 at 14:45
  • Yes, I also tried it with a getPackageName(). I also tried to changed the "stage"+counter to "stage"+counter+".png". Still not working – depecheSoul Jun 23 '15 at 14:47
  • Nope you don't have to specified file extension, here id is only be as your R.java file entries. – user370305 Jun 23 '15 at 14:49
  • You are displaying the next image of whatever counter values is, so in your case it should be like `"stage"+(counter+1)`. Also I suggest you to print `resourceId` before you will set it as image resource. – user370305 Jun 23 '15 at 14:55
1

You'll need to tell the vm to update -> 'invalide()'.

Or use Picasso,

Picasso.with(Statics.context).load(R.drawable.stageX).error(R.drawable.error_img).resize(width, height).priority(Priority.HIGH).into(image);
Danielson
  • 2,605
  • 2
  • 28
  • 51