Suppose your images are called img1.png, img2.png, etc., and they are in the res / drawable folder.
Then you can use the following code to randomly set the image in ImageView
ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(n) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
source
share