Show image randomly when button is clicked - java

I made 9 imagebuttons and 1 button.
I want only 4 buttons shows images when button is clicked, and it shows randomly every time button is pressed, how will that be possible?

try this
// add your image buttons to this list
ArrayList<ImageButton> allImageButtons = new ArrayList<ImageButton>();
allImageButtons.add(imgbtn1); //etc... for all 8 image buttons
Then in your button click method
Random rnd = new Random();
ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
while (randomNumbers.size() < 4)
{
int num = rnd.nextInt(9);
if (!randomNumbers.contains(num))
randomNumbers.add(num);
}
for (int i=0;i<allImageButtons.size();i++)
{
if (randomNumbers.contains(i))
allImageButtons.get(i).setVisibility(View.VISIBLE);
else
allImageButtons.get(i).setVisibility(View.GONE);
}

You can create an array that include image's src name and you can use
string imageArray = Name of yur images
int random = (int )(Math.random() * 9);
Then you can chance your image button images according to this random number.
imgButton.setBackgroundResource(imageArray[random]);
I hope it is clear and helpful.

Related

Display 3 random Images, without a double displayed one in Java?

I already saw a few questions regarding this, but none with java that I could understand, so here's my problem:
I have a an ArrayList called playableCards with 6 drawables:
ArrayList<Image> playableCards =new ArrayList<>(
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6);
In my Activity I have 3 ImageViews: imageView1 imageView2 imageView3
I need these 3 ImageViews to each display one drawable from playableCards, but none should be displayed twice.
The User can click each ImageView and by doing so, one of the remaining drawables of playableCards should replace the drawable before.
So if the User clicks imageView1, which currently displays a random drawable, lets say R.drawable.image3, it should change to another drawable of playableCards that isn't displayed in the other 2 ImageViews...
Does someone know how that is possible?
you can do that using this random selection like below:
ArrayList<Image> playableCards =new ArrayList<>(
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6);
Image[] selected = new Image[3];
void randomSelect(){
for(int i=0; i<3; i++){
int randomSelectedIndex = new Random().nextInt() % (6-i);
selected[i] = playableCards.get(randomSelectedIndex);
playableCards.remove(randomSelectedIndex);
}
}
void onClick(int clickedItemIndex){
int randomSelectedIndex = new Random().nextInt() % 3;
Image temp = selected[clickedItemIndex];
selected[clickedItemIndex] = playableCards.get(randomSelectedIndex);
playableCards.set(randomSelectedIndex,temp);
}

How to set image of a button randomly?

I'am trying to do memory game. I have 12 buttons and 6 images.
I want to randomly set the image to the button. One image to 2 buttons.
ImageIcon[] icons = {icon1,icon2,icon3,icon4,icon5,icon6};
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
I know this Random r = new Random(); but no idea how can I use it here.
Edit:
U used this
for (int i = 0; i < buttons.length; i++)
buttons[i].setIcon( iconList.get(i) );
How can I set something like visible(false) of this icon?
Don't have 12 different variable names for your buttons. Instead create an Array to hold your 12 buttons.
Use an ArrayList to contain 12 Icons (two of each image).
Then you can use the shuffle(...) method of the ArrayList to randomly sort the icons.
Then you create a loop to assign each Icon to a button. Something like:
for (int i = 0; i < buttons.length; i++)
buttons[i].setIcon( iconList.get(i) );
Edit:
The above suggestion was to assign the Icons to the button when the button was created.
If you have a game where you have an empty Icon and then you display the Icon when the button is clicked then you need a different approach.
In your ActionListener code you will need to search the button array to see which button was clicked. Once you get the index of this button, then you get the matching Icon:
JButton button = (JButton)event.getSource();
for (int i = buttons.length; i < buttons.length;i++)
{
if (button = buttons[i];
{
button.setIcon( iconList.get(i) );
break;
}
}
The same ActionListener can be used for all buttons since the logic is generic.

How to make a Random Layout when button clicked

Actually i want to make it Random class but then i think to much activity can make the app slow so i just want to make Relative layout Random
so i have 5 layout in one activity class
layout1 = (RelativeLayout)findViewById(R.id.layout1);
layout2 = (RelativeLayout)findViewById(R.id.layout2);
layout3 = (RelativeLayout)findViewById(R.id.layout3);
layout4 = (RelativeLayout)findViewById(R.id.layout4);
layout5 = (RelativeLayout)findViewById(R.id.layout5);
and in each layout there is the button in there to make layout random again
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//The code to how make layout random
}
});
}
and then how to make layout that already opened not open again if the button random was pressed? then if all layout was already opened it will open new activity class
can anyone help me explain with give some example code of that?
Initially set visibility gone to all relative layouts and put all of them into View's ArrayList.
Get random number from 0 to List size.
Get View at random position and set its visibility to Visible and remove from ArrayList.
Do same thing until ArrayList is empty.
Create new activity when ArrayList is empty.
Code:
ArrayList<View> viewList=new ArrayList<>();
initLayouts(){
layout1 = (RelativeLayout)findViewById(R.id.layout1);
layout2 = (RelativeLayout)findViewById(R.id.layout2);
layout3 = (RelativeLayout)findViewById(R.id.layout3);
layout4 = (RelativeLayout)findViewById(R.id.layout4);
layout5 = (RelativeLayout)findViewById(R.id.layout5);
viewList.add(layout1);
viewList.add(layout2);
viewList.add(layout3);
viewList.add(layout4);
viewList.add(layout5);
for(int i=0;i<viewList.size();i++){
viewList.get(i).setVisibility(View.GONE);
}
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
loadRandomLayout();
}
});
}
public loadRandomLayout(){
if(viewList.size()>0) {
Random r = new Random();
int number = r.nextInt(viewList.size());
viewList.get(number).setVisibility(View.VISIBLE);
viewList.remove(number);
}else{
startActivity(new Intent(this,NewActivity.class));
}
}
You could create random int as follows:
//To get a Random number 1-5 (I saw your RelativeLayouts and you've 5
Random rand = new Random();
int randomNum = rand.nextInt((5 - 1) + 1) + 1;
And then you could create a method to choose what to show :
public void ShowRelativeLayout(int rand){
switch(rand){
case 1:
if (layout1.getVisibility() == View.VISIBLE) {
//Do nothing cause it's visible
break;
} else {
layout1.setVisibility(View.VISIBLE);
break;
}
case 2:
..........
}
Make an array to store the layout indexes.
RelativeLayout[] layout = new RelativeLayout[5];
layout[0] = (RelativeLayout)findViewById(R.id.layout[0]); // 0
layout[1] = (RelativeLayout)findViewById(R.id.layout[1]); // 1
layout[2] = (RelativeLayout)findViewById(R.id.layout[2]); // 2
layout[3] = (RelativeLayout)findViewById(R.id.layout[3]); // 3
layout[4] = (RelativeLayout)findViewById(R.id.layout[4]); // 4
Make a simple random number generator.
public void FindNewLayout ()
{
Random r_generator = new Random();
int randomNum;
//now the only way to know which layouts have been shown before, you
//need to store the indexes that have been used before, somewhere.
//I recommend using an array.
// count, and the array below should be initialized somewhere else
//rather than inside the method so that only one instance of each is
//created, but for simplicity I'll just put their initialization here
int static count = 0;
//I'll explain below what count does.
// the log array that remembers each layout change
boolean[] log = new boolean[5];
do
{
//select new random number
randomNum = r_generator.nextInt((max - min) + 1) + min;
//in this case max = 4, min = 0, so replace these values in the
//equation above
// check the log to see if the number has appeared again
if ( log[randomNum] == false )
{
//Great! it hasn't appeared before, so change layout
log[randomNum] = true;
layout[randomNum].setVisibility = true;
count++; // increases step
break; //stops while because an unused layout has been found
}
}while (count<5)
//if the value of count is equal to 5 then every layout has been used
//before so the do-while code should not be run again
}// end method
And the above method should be called whenever you want to try to change layout.
Finally, you can use something like the Debugger.log("message"); statement
to be printed on the console for debugging purposes if you want, in order to find out when the layout has changed.

Switch button text value in Android Studio

Hello stackoverflow community,
I decided to start learning how to make Android Apps in Android Studio and as a first project i thought it would be fun to make a simple calculation game.
First it generates a random sum, for example 5 + 5.Underneath the sum It has 4 buttons on which i want to generate the correct answer and three wrong answers. The player presses the button with the correct or wrong answer and the sum and the answers get generated again.
// Generate wrong answers and convert
int wronganswer1 = (answ1) + 2;
String wronganswer1string = Integer.toString(wronganswer1);
int wronganswer2 = (answ1) - 2;
String wronganswer2string = Integer.toString(wronganswer2);
int wronganswer3 = (answ1) + 3;
String wronganswer3string = Integer.toString(wronganswer3);
//Add Text to the buttons
Button ansb1 = (Button)findViewById(R.id.answerbutton1);
ansb1.setText(answer);
Button ansb2 = (Button)findViewById(R.id.answerbutton2);
ansb2.setText(wronganswer1string);
Button ansb3 = (Button)findViewById(R.id.answerbutton3);
ansb3.setText(wronganswer2string);
Button ansb4 = (Button)findViewById(R.id.answerbutton4);
ansb4.setText(wronganswer3string);
This fills the text on the buttons with one correct answer and three wrong answers. The problem is that the correct answer will always be the button at the top.
My question is how to switch the values of the buttons each time a new sum is generated. So the correct answer wont always be on the same button.
Use an array to hold your answers.
String[] ans = new String[4];
int wronganswer1 = (answ1) + 2;
int wronganswer2 = (answ1) - 2;
int wronganswer3 = (answ1) + 3;
ans[0] = Integer.toString(answ1);
ans[1] = Integer.toString(wronganswer1);
ans[2] = Integer.toString(wronganswer2);
ans[3] = Integer.toString(wronganswer3);
Save your Button references to a List.
List<Button> btns = new ArrayList<Button>(4);
btns.add((Button)findViewById(R.id.answerbutton1));
btns.add((Button)findViewById(R.id.answerbutton2));
btns.add((Button)findViewById(R.id.answerbutton3));
btns.add((Button)findViewById(R.id.answerbutton4));
Now, use Collections.shuffle() to randomize the Buttons
Collections.shuffle(btns);
and just iterate over the List to set the answers.
for (int i = 0; i < 4; i++) {
btns.get(i).setText(ans[i]);
}
You should set up a randomized on the buttons so the strings would intercept the calculations so it would be like if (right answer) is # go to button #, wrong answer is #-# go to button #

Android: set button to random position

How can I as easy as possible set the position of my Button to a random place?
I've tryed to use this: (My screen resolution is 800x480)
Button btn = (Button) findViewById(R.id.button1);
Random r = new Random();
int x = r.nextInt(480);
int y = r.nextInt(800);
btn.setX(x);
btn.setY(y);
But when i use this, it seems that the button some times get located outside my application or so? is it possible to prevent this and keep the button inside the app?
Pretty sure your problem is you are not accounting for the width and height of the button, and so if it randoms to 480*800 it will be off the screen. Try something similar to:
int x = r.nextInt(480 - buttonWidth);
int y = r.nextInt(800 - buttonHeight);

Categories

Resources