Android/Java "Convert" String to Button - java

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");

Related

Randomly place a text on some buttons

I am trying to move, say a text or number in setText(Integer.toString()) format randomly on 4 different buttons at each click. I am able to obtain the object form of it, but it is worthless since I can't get that information out of it.
This is my code:
public void clickButton(View view){
List mo = new ArrayList();
mo.add(Button);
mo.add(Button1);
mo.add(Button2);
mo.add(Button3); // these are defined in OnCreate method
Random rko = new Random();
int koo = rko.nextInt(mo.size());
Object a = mo.get(koo);
Log.i("here", String.valueOf(mo.get(koo)));
I get this output in the logs up on each click
android.support.v7.widget.AppCompatButton{71c6fe0 VFED..C.. ...PH... 697,708-928,933 #7f0c0052 app:id/Button2}
android.support.v7.widget.AppCompatButton{71c6fe0 VFED..C.. ...PH... 697,708-928,933 #7f0c0052 app:id/Button3}
android.support.v7.widget.AppCompatButton{71c6fe0 VFED..C.. ...PH... 697,708-928,933 #7f0c0052 app:id/Button}
I am new to this. Please help
Thanks.
hi i believe that u should use tags on your buttons p = new Random().nextInt(21);
button.setTag(Integer.toString(alpha));
//here alpha is the text that you wanted to put on the button,get the text from button by using appropriate code
cbutton.setTag(Integer.toString(" "));
You could try writing AppCompatButton instead of Object and then you should be able to get the text of a button with a.getText(); and change the text of a button with a.setText();.
#Rushikesh You did not restrict the random generated number within the range of 0-4.
The size of array list of button is 4 so the the index must be 0-4 to avoid the IndexOutOfBoundsException
List mo = new ArrayList();
mo.add(Button);
mo.add(Button1);
mo.add(Button2);
mo.add(Button3);
int koo = (int)(Math.random() * (10-(mo.size()+1))); // generated number range will be 0-4
Object a = mo.get(koo);

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 #

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: 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);

A very strange for loop bug in Java for Android?

I'm trying to alter the attributes of an array of Buttons, however I am getting some very strange errors. I am trying to loop through the buttons to edit the height attribute of each one, however when I set up a for loop (i=0; i<3; i++), buttonSkater[i].setHeight(buttonHeight); the result seems to be that 9 buttons are being altered! And when I set i<14 (there are 14 buttons), then the application crashes with a NullPointerException.
package com.rollerderby.lineuptracker;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
public class Setup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup);
Button[] buttonSkater = new Button[14];
buttonSkater[0] = (Button) findViewById(R.id.buttonSkater1);
buttonSkater[1] = (Button) findViewById(R.id.buttonSkater2);
buttonSkater[2] = (Button) findViewById(R.id.buttonSkater3);
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int buttonHeight = (screenHeight-60)/14;
for(int i=0; i<14; i++)
{
buttonSkater[i].setHeight(buttonHeight);
}
}
}
It seems a very strange error... Am I missing something obvious?
(Sizing the array wrong by creating it with space for new Buttons?)
Based purely on your code example, you're trying to access the 3rd to 14th element in an array that has only 3 indexes set (0 to 2), the other ones are (by default) initialized to null. As for the 14 buttons, are they 14 instances of just 3 buttons perhaps?
#fwielstra's answer is the right one, but for posterity, I thought I'd add some improvements to your code to help with resolving the problem in the future. Instead of doing this sort of code which is prone to the sort of error you had:
Button[] buttonSkater = new Button[14];
buttonSkater[0] = (Button) findViewById(R.id.buttonSkater1);
buttonSkater[1] = (Button) findViewById(R.id.buttonSkater2);
buttonSkater[2] = (Button) findViewById(R.id.buttonSkater3);
Instead you can initialize the size of the array like:
Button[] buttonSkater = new Button[] {
(Button) findViewById(R.id.buttonSkater1),
(Button) findViewById(R.id.buttonSkater2),
(Button) findViewById(R.id.buttonSkater3),
};
And when you are processing the array, instead of doing the following which means that if you change the size of the array, you need to do it in multiple places:
for (int i = 0; i < 14; i++)
...
I would use:
for (int i = 0; i < buttonSkater.length; i++)
...
Best of luck.
Without access to your XML, my guess is, if with a loop until 3 you're changing other 9 buttons it's because their id might be the same (i.e., copy/paste mistakes).
Also, from your code, it seems normal your application would crash with your loop until 14; after all, only the first 3 elements were instantiated. Once it gets to the 4th, it should access a null value, thus causing an error.
Well, for starters, your "14" loop is failing because only positions 0-2 have been assigned to anything. In Java, when you do
String[] strings = new String[5];
Nothing but the array has actually been initialized so
strings[0] == null;
and so on until you've actually initialized those values.
You need to manually initialize each value with
strings[0] = new String();
strings[1] = new String();
...
We'll need to see the other code to answer your question about modifying nine buttons.

Categories

Resources