user1731690
user1731690

Reputation: 225

Random text for button in android

Example I have 3 buttons

<Button
    android:id="@+id/q1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/q2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/q3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

and I have text to random as bellow

private static final String[] answers = {"a", "b", "c", "Vowel", "d", "e", "f"};
String q = answers[rgenerator.nextInt(answers.length)];
button1.setText(q);
button2.setText(q);
button3.setText(q);

Example I start activity 5 times to random:

1st activity start random look like: a, c, Vowel
2st activity start random look like: b, Vowel, a
3st activity start random look like: Vowel, e, b
4st activity start random look like: a, e, Vowel
5st activity start random look like: Vowel, b, f

All the time activity start must have "Vowel" setText to the 1 of 3 buttons all the times when activity refresh/start.

I am really stuck on it..

Upvotes: 1

Views: 1180

Answers (2)

Michael Shrestha
Michael Shrestha

Reputation: 2555

Your can try this for both case:

 ArrayList<Integer> numbers = new ArrayList<Integer>();
 boolean vowel_added = false;
    Random rnd = new Random();
    while (numbers.size()<=3) {
      int randomInteger = rnd.nextInt(answers.length());
      if (!numbers.contains(randomInteger)) {

            if (answers[randomInteger]).equalsIgnoreCase("Vowel"))
             {
                 if (! vowel_added)
                 {
                   vowel_added = true;
                   numbers.add(randomInteger);
                 }
             }
             else
             {
                 numbers.add(randomInteger);
             }
       }
    }
    if (! vowel_added) {
      int index = rnd.nextInt(numbers.size());//this random to set the index of vowel at random position
      numbers.set(index,3);//you can find the index of one vowel and put here
    }
    button1.setText(answers[numbers.get(0)]);
    button2.setText(answers[numbers.get(1)]);
    button3.setText(answers[numbers.get(2)]);

hope this helps

Upvotes: 7

Yahya Arshad
Yahya Arshad

Reputation: 1626

int firstRandomNum  = (int)(Math.random()*6);  // Generate Random Number
int secondRandomNum = (int)(Math.random()*6);  // Generate Random Number

int indexOfVowel = 3; // As you said one text must be Vowel , its index is 3

Button b1 = (Button)findViewById(R.id.q1);
Button b2 = (Button)findViewById(R.id.q2);
Button b3 = (Button)findViewById(R.id.q3);
if(firstRandomNum != indexOfVowel && secondRandomNum != indexOfVowel ) {

 b1.setText(""+answers[firstRandomNum]);
 b2.setText(""+answers[secondRandomNum ]);
 b3.setText(""+answers[indexOfVowel]);
} else {
    b1.setText(""+answers[firstRandomNum]);
    b2.setText(""+answers[secondRandomNum ]);
    b3.setText(""+answers[indexOfVowel+1]);
}

above code is just to help you, it is not complete logic or error free

Upvotes: 0

Related Questions