0% found this document useful (0 votes)
5 views4 pages

Randomize Images

The document describes an Android application that displays four randomized images in boxes. Tapping a box reveals the image, and tapping a button re-randomizes the images. The app uses arrays of images and numbers to assign random images to each box on button press.

Uploaded by

Amber Green
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Randomize Images

The document describes an Android application that displays four randomized images in boxes. Tapping a box reveals the image, and tapping a button re-randomizes the images. The app uses arrays of images and numbers to assign random images to each box on button press.

Uploaded by

Amber Green
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ID: img1

ID: img2

ID: img3

ID: img4

ID: btnNext
Every time you tap on button NEXT, the images in the four boxes
will be randomized and covered with question mark.

Tap the box to open/view the animal inside.

package com.example.randomize_images;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {


ImageView imgOne,imgTwo,imgThree,imgFour;
Button btnNext;
//declare an integer variable which will hold an array or list of number 1-16
List<Integer> arr = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgOne = findViewById(R.id.img1);
imgTwo= findViewById(R.id.img2);
imgThree= findViewById(R.id.img3);
imgFour= findViewById(R.id.img4);
btnNext = findViewById(R.id.btnNext);

//this will loop 16 times, and everytime it loops the number stored in arr variable
using the arr.add
for (int i = 1; i <= 16; ++i) arr.add(i);

btnNext.setOnClickListener(v -> {
getRandomNumber();
});

imgOne.setOnClickListener(v -> {
//call flipImageBox and provide the number of the image button being pressed
flipImageBox(1);
});
imgTwo.setOnClickListener(v -> {
//call flipImageBox and provide the number of the image button being pressed
flipImageBox(2);
});
imgThree.setOnClickListener(v -> {
//call flipImageBox and provide the number of the image button being pressed
flipImageBox(3);
});
imgFour.setOnClickListener(v -> {
//call flipImageBox and provide the number of the image button being pressed
flipImageBox(4);
});
}

//create an array of object representing your drawables/images


private static final Integer[] img_array = {
R.drawable.animal_1,R.drawable.animal_2,R.drawable.animal_3,R.drawable.animal_4,
R.drawable.animal_5,R.drawable.animal_6,R.drawable.animal_7,R.drawable.animal_8,
R.drawable.animal_9,R.drawable.animal_10,R.drawable.animal_11,R.drawable.animal_1
2,
R.drawable.animal_13,R.drawable.animal_14,R.drawable.animal_15,R.drawable.animal_
16,
};

void flipImageBox(int btn){


//declare a drawable variable named new_image
Drawable new_image;
switch (btn){
case 1:
//set the drawable image of the button using the array of images on your
drawable with index 0-3
// only as you have 4 image buttons.
new_image = getResources().getDrawable(img_array[arr.get(0)], getTheme());
imgOne.setImageDrawable(new_image);
break;
case 2:
new_image = getResources().getDrawable(img_array[arr.get(1)], getTheme());
imgTwo.setImageDrawable(new_image);
break;
case 3:
new_image = getResources().getDrawable(img_array[arr.get(2)], getTheme());
imgThree.setImageDrawable(new_image);
break;
case 4:
new_image = getResources().getDrawable(img_array[arr.get(3)], getTheme());
imgFour.setImageDrawable(new_image);
break;
}
}
void getRandomNumber(){
//shuffle the array of numbers so that there will always be a new set of first 4
images in the array list
//everytime you click on NEXT Button
Collections.shuffle(arr);
//set the images to default question mark
imgOne.setImageResource(R.drawable.def_000);
imgTwo.setImageResource(R.drawable.def_000);
imgThree.setImageResource(R.drawable.def_000);
imgFour.setImageResource(R.drawable.def_000);
}
}

You might also like