Switch button text value in Android Studio - java

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 #

Related

How to add multiple lines in Label JavaFX

I have a problem that I don't really know how to add a multiple lines into Label in JavaFX.
For example:
Label label = new Label();
for(int i= 0; i<10; i++){
label.setText(Integer.toString(i));
}
So when the loop finishes, the label just only shows the final value which is 9.
So any solutions that can show all the numbers 1 - 9 with the break lines( such as '\n') between them.
This problem that happens when i want to show the Bill of my project that contain many dishes.
Thank you for your help.
You need to append and not set the text over and over again
AND you need the new line character '\n'
my suggestion would be like using a variable to append the information and when you are done with that step, then set the label.text
Example:
StringBuilder msg = new StringBuilder():
Label label = new Label();
for (int i = 0; i < 10; i++) {
msg.append(Integer.toString(i));
msg.append(",\n"); //this is the new line you need
}
label.setText(msg.toString());

Show image randomly when button is clicked

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.

Android/Java "Convert" String to Button

I have this code:
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
String object = "button";
int num;
num = r.nextInt(3 - 1) + 1;
String total = object + num;
I want do set the text for one of the buttons chosen randomly. Something like this:
button<num>.setText(some_text);
^ here instead of <num> should be 1 or 2
and has to be chosen randomly
Like Ondkloss said, you can add your buttons to an array, then randomly select one from that array.
Button[] buttonArray = new Button[2];
buttonArray[0] = button1;
buttonArray[1] = button2;
Random r = new Random();
buttonArray[r.nextInt(2)].setText(someRandomText);
Keep in mind that if you change the number of buttons you will need to change the numbers that I have used (new Button[2] & r.nextInt(2)). My solution works specifically for an array of length 2 containing only 2 buttons. But other than changing the numbers in the array creation and the random number generation to match the number of buttons you have, this solution should work just fine.
No, i want change the text of the button.
Then just do something like this
button1.setText("Just some strings here");

How to find out which textfield was typed into by the user in a Java JtextField?

I have the following code:
int[] matrix = new int[9][9];
(for int x = 0; x <= 8; x++){
(for int y = 0; y <= 8; x++){
JtextField matrix[x][y] = new JtextField(“Matrix" x + y)
int[] coords = new int[2];
coords[0] = x;
coords[1] = y;
matrix[x][y].putClientProperty("coords", coords);
matrix[x][y].setText(game[x][y]);
}
}
After the loops end, I need a way of finding out which textfield the user typed into (the user also hits enter). So:
1. How do I check if a JtextField has been edited without knowing which one it is or its name?
2. How do I check which "coords" the textfield is positioned at? I have an idea of how to do it, but for some reason I cannot code it.
I feel like the answer is staring me right in the face, but its late, I'm tired, and I'm getting flustered. Thanks!
The answer depends on what you want to achieve.
If you're only interested in the end result, you could use an ActionListener (for when the user hits Enter) and a FocusListener for when they don't and leave the field any way (assuming you want to know this)
When the respective event occurs, you can inspect the source of the event by using getSource. Now this returns Object, but you can use instanceof to determine if the object can be cast to a JTextField...
For example...
Object source = evt.getSource();
if (source instanceof JTextField) {
JTextField field = (JTextField)source;
int[] coords = field.getClientProperty("coords");
}
Take a look at How to write an Action Listener and How to write a Focus Listener for more details.
Depending on your needs, you could also take a look at Validating Input

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