DataStructors23
DataStructors23

Reputation: 137

Get random images

I am trying to create a card game for an android app (for practice) and I was wondering how would I upload random cards into the players hand? I have the GUI implemented and I think I would need something along the lines of a typedarray and

   ((ImageView)findViewById(R.id.textView18)).setImageResource(R.drawable.1c);

Am I moving along the right tracks with this?

Upvotes: 3

Views: 10043

Answers (1)

SpiralDev
SpiralDev

Reputation: 7321

You can get random images like this:

int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Random rand = new Random();
imageView.setImageResource(images[rand.nextInt(images.length)]);

Upvotes: 12

Related Questions