I am building an app that when a button is pressed a random image in a gridLayout disappears revealing something behind it.
There are 28 images in the gridLayout.
I have my images in an array and inside the buttons onClick i want to get a random image from that array and set its visibility to invisible.
I appreciate if anyone can help.
Here is my code:
public class MainActivity extends AppCompatActivity {
ImageView image1;
ImageView image2;
ImageView image3;
ImageView image4;
ImageView image5;
ImageView image6;
ImageView image7;
ImageView image8;
ImageView image9;
ImageView image10;
ImageView image11;
ImageView image12;
ImageView image13;
ImageView image14;
ImageView image15;
ImageView image16;
ImageView image17;
ImageView image18;
ImageView image19;
ImageView image20;
ImageView image21;
ImageView image22;
ImageView image23;
ImageView image24;
ImageView image25;
ImageView image26;
ImageView image27;
ImageView image28;
Button revealButton;
Random rand;
Integer[] imagesArray = {
R.id.imageView1,
R.id.imageView1,
R.id.imageView2,
R.id.imageView3,
R.id.imageView4,
R.id.imageView5,
R.id.imageView6,
R.id.imageView7,
R.id.imageView8,
R.id.imageView9,
R.id.imageView10,
R.id.imageView11,
R.id.imageView12,
R.id.imageView13,
R.id.imageView14,
R.id.imageView15,
R.id.imageView16,
R.id.imageView17,
R.id.imageView18,
R.id.imageView19,
R.id.imageView20,
R.id.imageView21,
R.id.imageView22,
R.id.imageView23,
R.id.imageView24,
R.id.imageView25,
R.id.imageView26,
R.id.imageView27,
R.id.imageView28
};
public void reveal(View view) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = findViewById(R.id.imageView1);
image2 = findViewById(R.id.imageView2);
image3 = findViewById(R.id.imageView3);
image4 = findViewById(R.id.imageView4);
image5 = findViewById(R.id.imageView5);
image6 = findViewById(R.id.imageView6);
image7 = findViewById(R.id.imageView7);
image8 = findViewById(R.id.imageView8);
image9 = findViewById(R.id.imageView9);
image10 = findViewById(R.id.imageView10);
image11 = findViewById(R.id.imageView11);
image12 = findViewById(R.id.imageView12);
image13 = findViewById(R.id.imageView13);
image14 = findViewById(R.id.imageView14);
image15 = findViewById(R.id.imageView15);
image16 = findViewById(R.id.imageView16);
image17 = findViewById(R.id.imageView17);
image18 = findViewById(R.id.imageView18);
image19 = findViewById(R.id.imageView19);
image20 = findViewById(R.id.imageView20);
image21 = findViewById(R.id.imageView21);
image22 = findViewById(R.id.imageView22);
image23 = findViewById(R.id.imageView23);
image24 = findViewById(R.id.imageView24);
image25 = findViewById(R.id.imageView25);
image26 = findViewById(R.id.imageView26);
image27 = findViewById(R.id.imageView27);
image28 = findViewById(R.id.imageView28);
revealButton = findViewById(R.id.reveal);
rand = new Random();
}
}
CodePudding user response:
you can use nextInt method like the code below
int randomImagePosition= new Random().nextInt(imagesArray.length);
int imageId = imagesArray[randomImagePosition] ;