I am trying to make 2 different view for user that is one for recommendation i.e (to show one single button) and another for just all information. I need the app to call a random button among all the other button every time i restart the app.
In my opinion, what is better is to have a single button and every time you restart the app you pick a random behaviour for that button.
There are several ways to do it this is just one:
public class MainActivity extends AppCompatActivity {
private static final String BUTTON_TEXT1 = "Button 1";
private static final String BUTTON_TEXT2 = "Button 2";
private static final String TEXTVIEW_TEXT1 = "Text to display #1";
private static final String TEXTVIEW_TEXT2 = "Text to display #2";
private String[] mButtonTexts = {BUTTON_TEXT1, BUTTON_TEXT2};
private String[] mTextViewTexts = {TEXTVIEW_TEXT1, TEXTVIEW_TEXT2};
private static final int NUM_OF_TEXT = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
Random random = new Random();
final int index = random.nextInt(NUM_OF_TEXT);
button.setText(mButtonTexts[index]);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(mTextViewTexts[index]);
}
});
}
}
As per #MarkPazon suggestion
1. Assign a Button(the random One )
2. Declare a function callRandom()
3. invoke the function callRandom() from Onclick event of the Random
Button
public void callRandom(){
//Generate Random Numbers
final int min = 0;
final int max = n; //n-no of random events
Random g=new Random();
final int random = g.nextInt((max - min) + 1) + min;
//Random number 0(inclusive)-9(inclusive)
switch(random){
case 1:
//Function 1 break;
case 2:
//Function 2 break;
case 3:
//Function 3 break;
case n:
//Function n break;
}
}
PS: Here The function calls a Random event on every Button Click.If You Wish to assign random function at the Time of App Starts then paste this with in Oncreate
//Generate Random Numbers
final int min = 0;
final int max = n; //n-no of random events
Random g=new Random();
final int random = g.nextInt((max - min) + 1) + min;
//Random number 0(inclusive)-9(inclusive)
Related
I'm trying to make a simple matched pairs app. The app starts with all TextViews having "#" as a text. When I press a text field, it changes to a letter which was stored for that field.
Anyway, I want to open two fields (I mean to change their strings) and compare them. If they are equal, they remain "opened" with new letters (e.g. both are "A"). In the case with equal letters, everything works fine, but I have difficulties when the letters differ.
If they are not equal, I want to make them to show themselves with new text for one second and after that to go to the previous sign (i.e. "#"). When I press the first field, it changes its text but when I go for the second field and if their stored letters are not equal, the second field doesn't change the text and the text from the first field goes back to the previous sign.
What do I need to do to show fields with new text for a short period of time if their Strings are not equal?
This is how my table looks:
public class MainActivity extends AppCompatActivity {
TextView textView;
int counterForPressedFields= 0;
int textViewForComparationCounter = 0;
TextView firstTextViewForComparation;
TextView secondTextViewForComparation;
int[] nonPressedFields= {1, 1, 1, 1};
int pressedField = 2;
int tag = 0;
public void show(View view) throws InterruptedException {
textView = (TextView) view;
tag = Integer.parseInt(textView.getTag().toString());
if (nonPressedFields[tag] == 1) {
nonPressedFields[tag] = pressedField;
if (counterForPressedFields< 2) {
textViewForComparationCounter += 1;
counterForPressedFields+= 1;
switch (tag) {
case 0:
textView = (TextView) findViewById(R.id.front1);
textView.setText("A");
break;
case 1:
textView = (TextView) findViewById(R.id.front2);
textView.setText("B");
break;
case 2:
textView = (TextView) findViewById(R.id.front3);
textView.setText("A");
break;
case 3:
textView = (TextView) findViewById(R.id.front4);
textView.setText("B");
break;
}
if (textViewForComparationCounter == 1) {
firstTextViewForComparation = (TextView) view;
firstTextViewForComparation = textView;
} else if (textViewForComparationCounter == 2) {
secondTextViewForComparation= (TextView) view;
secondTextViewForComparation= textView;
}
}
if(counterForPressedFields == 2){
if(firstTextViewForComparation.getText().toString().equals(secondTextViewForComparation.getText().toString())){
counterForPressedFields= 0;
textViewForComparationCounter = 0;
}else{
firstTextViewForComparation.setText("#");
secondTextViewForComparation.setText("#");
nonPressedFields[Integer.parseInt(firstTextViewForComparation.getTag().toString())] = 1;
nonPressedFields[Integer.parseInt(secondTextViewForComparation.getTag().toString())] = 1;
counterForPressedFields= 0;
textViewForComparationCounter = 0;
}
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Try using a handler. You can update your textViews back to "#" with its postDelayed method.
private final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
//update your textViews here
}
};
handler.postDelayed(runnable, millisecondsOfDelay);
Hope it helps!
Every time an user click on the button it has to show "John - Sue" or "Sue - John".
I tried with this code:
public class MyActivity extends Activity {
ArrayList<String> names = new ArrayList<String>();
int p1, p2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
names.add("John");
names.add("Sue");
Button b = (Button) findViewById(R.id.mybutton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
p1 = (int)Math.random();
if (p1 == 0)
p2 = 1;
else
p2 = 0;
String msg = names.get(p1) + " - " + names.get(p2);
AlertDialog msgbox = new AlertDialog.Builder(About.this).setTitle("Click here").setMessage(msg).create();
//msgbox.setPositiveButton("OK", null);
msgbox.setCancelable(true);
msgbox.show();
TextView textView = (TextView) msgbox.findViewById(android.R.id.message);
textView.setTextSize(16);
}
});
}
}
But i get always the same order, even i close and run again the app. How to do that?
If you want shuffle list:
Collections.shuffle(names)
If you want random int between 0 or 1 (nextInt(int) javadoc):
Random random = new Random();
int randomInt = random.nextInt(2);
Math.random() returns a number between 0 and 1. So when you cast it to int it will always be 0.
Try this:
p1 = (int)(Math.random()*2);
It happens because
p1 = (int)Math.random();
always gives you zero.
I'm a newbie android programmer.
I'm tring to create a simple game.
Game;
Displaying a random number bettween 1 to 10, let's say it mainnumber.
Displaying 4 buttons.
First button value is mainnumber.
Second button value is mainnumber + 1
Third button value is mainnumber - 1
and The Last button value is mainnumber + 2
How to play;
If the user clicked correct button (mainnumber), it'll display another number and buttons.
My Code;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easygame);
Button b1 = (Button)findViewById(R.id.answer_one);
Button b2 = (Button)findViewById(R.id.answer_two);
Button b3 = (Button)findViewById(R.id.answer_three);
Button b4 = (Button)findViewById(R.id.answer_four);
Random number = new Random();
int mainnumber = number.nextInt(10)+1;
int rnd1 = mainnumber + 1;
int rnd2 = mainnumber + 2;
int rnd3 = mainnumber - 1;
String a = Integer.toString(mainnumber);
String b = Integer.toString(rnd1);
String c = Integer.toString(rnd2);
String d = Integer.toString(rnd3);
((TextView) findViewById(R.id.display)).setText(Integer.toString(mainnumber));
List<Button> buttons = Arrays.asList(b1, b2, b3, b4);
List<String> texts = Arrays.asList(a, b, c, d);
Random rand = new Random();
buttons.get(rand.nextInt()).setText(texts.get(rand.nextInt()));
}
Getting system error, and app closing.
Question;
How can I display buttons randomly each time, and how can I check the clicked button is true button.
Thanks...
Collections.shuffle(texts);//shuffle the buttons
//shuffle the buttons if you want as well, but whatever..
int i = 0;
//for loop moved below..
/*
for(Button button : buttons)
{
button.setText(texts.get(i++));
}
*/
//custom android.view.View.OnClickListener instance.
OnClickListener onClick = new OnClickListener()
{
public void onClick(View view)
{
Button button = (Button) view;//safe cast since this is only added to buttons.
String value = button.getText();
//process button value..
}
};
for(Button button : buttons)
{
button.setText(texts.get(i++));
button.setOnClickListener(onClick);
}
In an android app I intend to allow users to answer a random sum then a new one appears on screen. This is repeated 10 times and then a final score will then be given. However I am unsure how to update the sum so that after each each a new random is shown on screen.
Below is my current code:
public class Test extends Activity {
//declare vars
TextView text;
EditText answer;
Button submit;
int random1;
int random2;
String question;
int correctAnswer;#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// initialising variables
initialiseVars();
//set up random
setUpRandom();
//Set text view equal to question
text.setText(question);
//updateQuestion?
}
public void initialiseVars() {
text = (TextView) findViewById(R.id.tvTopRandomTest);
answer = (EditText) findViewById(R.id.etEnterAnswerRandomTest);
submit = (Button) findViewById(R.id.btnSubmitRandomTest);
}
public void setUpRandom() {
//setting up randoms
Random random = new Random();
// Generating random number between 1 and 12
random1 = random.nextInt(12) + 1;
// Generating another random number between 1 and 12
random2 = random.nextInt(12) + 1;
question = random1 + " x " + random2 + " = ";
correctAnswer = random1 * random2;
}
public void updateQuestion() {
//CODE TO UPDATE QUESTION
}
}
Add Button ClickListener so that when user press submit button it will update question and clean all previous values
submit = (Button) findViewById(R.id.btnSubmitRandomTest);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateQuestion();
}
}
Maintain a count in your activity and increase it in updateQuestion
public void updateQuestion() {
if (Int.parseString(answer.getText().toString()) != correctAnswer) {
// Show toast or something
return;
}
tries++;
if (tries == 10) return; // or do something else;
answer.setText("");
setUpRandom();
text.setText(question); // add this line in your setUpRandom();
}
To generate random integers look at this. Hopefully this will help you out.
I am doing an application in which I have to display the numbers on TextView randomly and automatically with the help of Timer. I am able to get the random Numbers in the log without repeating, but I am not able to print the same on device please help me...
Regards,
Akki
Source:
//RandomNumber.java
public class RandomNumber extends Activity{
static Random randGen = new Random();
int tambolanum,count=0;
private Button previousbutton;
private Button startbutton;
private Button nextbutton;
int bingonum[]=new int[90];
boolean fill;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.numbers);
LinearLayout number=(LinearLayout)findViewById(R.id.numbersview);
final TextView randomnum=(TextView)findViewById(R.id.numberstext);
previousbutton=(Button)findViewById(R.id.previous);
nextbutton=(Button)findViewById(R.id.next);
startbutton=(Button)findViewById(R.id.start);
startbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on click
//--- Initialize the array to the ints 0-90
do{
fill = true;
//Get new random number
tambolanum = randGen.nextInt(90) + 1;
//If the number exists in the array already, don't add it again
for(int i = 0; i < bingonum.length; i++)
{
if(bingonum == tambolanum)
{
fill = false;
}
}
//If the number didn't already exist, put it in the array and move
//To the next position
if(fill == true)
{
bingonum[count] = tambolanum;
count++;
}
} while(count < 90);
for(i=0;i
{
randomnum.setText(Integer.toString(bingonum[i]);
}
}
setText(CharSequence text)
The problem you're having is that you're overwriting your text in every itteration of this loop:
for(i=0;i
{
randomnum.setText(Integer.toString(bingonum[i]);
}
You need to build your string first then set it. Something like:
StringBuilder sb = new StringBuilder();
for(i=0;i /* where's the rest of this for-statement? */
{
sb.append(Integer.toString(bingonum[i]);
}
randomnum.setText(sb.toString());