0

I have an imageView and a Button and trying to change different images in an image view by the click of a button. I know how to set an image in an image view that is by

 img.setBackground(getBackgroundResource(R.drawable.image1); 

but in my specific case i have 4 images and i want them to switch when the button is pressed. How can i do it?

Stan
  • 6,511
  • 8
  • 55
  • 87

6 Answers6

0

Use this

int counter=0;

public void onClick(View v){

    imageView.setImageResource(image_array[counter]);

   if(counter==3){
      counter=-1;
   }
   counter++;
}
Sandeep Singh
  • 1,117
  • 2
  • 11
  • 29
0

For this try the following steps one by one in your code 1) keep your images in an array like

int imagearray[]=`{R.drawable.image1,R.drawable.image2....};`

2) declare a variable int count=0;

3) inside the onclick of your button try the following

  count++;
  imageView.setImageResource(imagearray[count]);
Karthika PB
  • 1,373
  • 9
  • 16
0
final Button button1 = (Button) findViewById(R.id.button1_id);
            button1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   
                    img.setBackground(getBackgroundResource(R.drawable.image1);

                }
            });

final Button button2 = (Button) findViewById(R.id.button2_id);
            button2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   

             img.setBackground(getBackgroundResource(R.drawable.image2);

                }
            });

final Button button3 = (Button) findViewById(R.id.button3_id);
                button3.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // Perform action on click   

          img.setBackground(getBackgroundResource(R.drawable.image3);

                    }
                });
A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
0

for example with an integer array and a global variabel:

  private int[] drawables = {R.id.image1,R.id.image2,R.id.image3,R.id.image4};
  private int clicks = 0;

  yourButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View v){

        if(clicks<drawables.length-1){
          clicks++;

        }
          else{

              clicks=0;
         }
         img.setImageResource(drawables[clicks]);
       }

    });

Should just give a possibility, check the code if works correct. I can´t check it for now, but it should give You a simple idea.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

Suppose you have 4 images in arraylist as,

a=new ArrayList<String>();

a.add("R.drawable.image1");
a.add("R.drawable.image2");
a.add("R.drawable.image3");
a.add("R.drawable.image4");

Button button1 = (Button) findViewById(R.id.button1_id);
            button1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   
             Collections.shuffle(a);
             img.setBackground(getBackgroundResource(a.get(0));

                }
            });
Divya
  • 182
  • 1
  • 5
  • 16
0
int clickCounter = 0;
private int[] drawables = {
    R.id.image1,
    R.id.image2,
    R.id.image3,
    R.id.image4
};

public void myOnClick(View v) {
    clickCounter++;
    switch (clickCounter) {
        case 1:
            img.setImageResource(drawables[clickcounter]);
            break;
        case 2:
            img.setImageResource(drawables[clickcounter]);
            break;
        case n:
            break;
    }
}
serifsadi
  • 603
  • 7
  • 21