-3

Is there any method available for suppose if I click the button than the image should be change. Number of times I click the button the image should be change.

  • You can go through the images list or wherever the images are saved using a button click listener event. for example: if you have a list containing images URLs you can start looping from the first position in the list which is index = 0, go on until you reach the last index, then go back to index 0. – Khaled Almanea Nov 20 '19 at 18:20
  • You can create an integer that increment everytime you click the button then write an if statement based on the number of clicks then change the ImageView based on your condition. – L2_Paver Nov 21 '19 at 02:10
  • @L2_Paver thats exactly what i did. I have used random number and switch case. The number of time i click the button one random number will be generated and based on that it will change the image of image view. But i dont know which method is used for button. For suppose i want to change the image when i click the image than I have writtrn like this `myImageView.setDrawbleResource(R.drawble.imagename)` – Maitri Patel Nov 21 '19 at 02:54
  • @KhaledAlmanea I have taken a random number and switch case. Based on that numbers images must be changed. The number of times i click the button one random number will be generated and based on that it will change the image of imageview. – Maitri Patel Nov 21 '19 at 02:59

1 Answers1

0

I suppose you want to change the image of an ImageView

For changing the image of an ImageView, inside the onClick() of the Button use:

myImageView.setBackgroundResource(R.drawable.my_image_name);

Here, myImageView is the object bound an ImageView, and
R.drawable.my_image_name is the location of the image file present in your Drawable folder.

For more details refer to this thread

Update

I have use a random number and switch case. When I click the button the number will be generated and pass to switch case and based on that the image should appear in the image view.

In that case, let's assume you have 3 images in your drawable folder.
You can use a HashMap where the key is the index and the value is the image-path

HashMap<Integer, Integer> imageMap=new HashMap<Integer, Integer>();
imageMap.put(1,R.drawable.my_image4);
imageMap.put(2,R.drawable.my_image3);
imageMap.put(3,R.drawable.my_image2);

Now, generate Random number in range 1-3

int min = 1;
int max = 3;
int randomNumber = new Random().nextInt((max - min) + 1) + min;

Finally, set the image at the randomly generated position.

int randomImageId = imageMap.get(randomNumber);
myImageView.setBackgroundResource(randomImageId);

Hope this helps

saintlyzero
  • 1,632
  • 2
  • 18
  • 26
  • thanks for your answer...I want that when I click the button I want that image to be change – Maitri Patel Nov 20 '19 at 18:34
  • @MaitriPatel That's exactly what I answered. Add the above-provided code inside the `onClick()` function of your `button` – saintlyzero Nov 20 '19 at 18:49
  • I have use a random number and switch case. When i click the button the number will be generated and pass to switch case and based on that the image should be appear in the image view. – Maitri Patel Nov 21 '19 at 03:02
  • @MaitriPatel I have updated the answer. Hope that solves your problem – saintlyzero Nov 21 '19 at 06:46