-1

I have this Image Array which consists of 10 images.

int[] MyArray = new int[10];{
    MyArray[0] = R.drawable.a;
    MyArray[1] = R.drawable.b;
    MyArray[2] = R.drawable.c;
    MyArray[3] = R.drawable.d;
    MyArray[4] = R.drawable.e;
    MyArray[5] = R.drawable.f;
    MyArray[6] = R.drawable.g;
    MyArray[7] = R.drawable.h;
    MyArray[8] = R.drawable.i;
    MyArray[9] = R.drawable.j;
}

I have an image view in my activity which i want to fill with a random image from the image array. How do I go about doing this, any help?

To view an image from the array in an image view I use this code

ImageView ImgView = (ImageView)findViewById(R.id.imageView);
ImgView.setImageResource(MyArray);
Raj
  • 35
  • 6

1 Answers1

1
Random random = new Random();
int indexToGetImageFrom = random.nextInt(sizeOfYourArray);

The above code will generate a random number for you. nextInt method of Random class generates a number between 0(inclusive) and the parameter given (exclusive).

Sneh
  • 3,527
  • 2
  • 19
  • 37
  • thanks for the random generator, but how will i tell my Image View select the image from the image array with the random numbers as the id, would i use NameOfMyImageView.setImageRescources():? – Raj Jan 04 '16 at 09:56
  • Well I am not sure about that as the code which you have given, I can't add much more to it. I would advise you to ask another question for the same. – Sneh Jan 04 '16 at 09:59
  • i editied the question if that helps – Raj Jan 04 '16 at 10:10
  • I think you should ask another question about it, as I am no android expert :) I helped you with the initial question which was generating random numbers. If you feel it helped, so accept the solution. – Sneh Jan 04 '16 at 13:53