0

I have a button and a few strings.

How do I make that when I click the button, a string changes random to another string? I'm not sure what part of code I should copy in here...

Button:

 <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/txt_example1"
      android:layout_centerHorizontal="true"
      android:background="@drawable/button_newfact"
      android:minHeight="38dip"
      android:minWidth="74dp"
      android:onClick="OnButtonClickChangeTextOfTextView" />

This is something in the MainActivity.java I made with a YouTube tutorial. But it only changes to a new text, and not another string. And it isn't random.

 public void OnButtonClickChangeTextOfTextView(View view)
    {
        TextView textView = (TextView)findViewById(R.id.txt_example1);
        textView.setText("");
    }

I hope you can help me. I wish you all a nice third advent. :)

Andy
  • 61,948
  • 13
  • 68
  • 95
  • Which random strings do you want? Do you have a list of random string or something? What do you mean by _"But it only changes to a new text, and not another string"_? _text_ and _string_ are synonymous in case of text view.. – Amulya Khare Dec 15 '13 at 14:37
  • You will have to qualify the word "random" here. Do you mean you want to choose a random string from a pre-existing array of strings? – nhgrif Dec 15 '13 at 14:38
  • Okay. So I have made 5 strings with different texts. And I want that when I click the button, that there will appear one of the strings in the list. – user3104453 Dec 15 '13 at 14:41
  • Can you show how this list looks like.. is it array? arraylist or what? update the question.. – Amulya Khare Dec 15 '13 at 14:43
  • I don't have enough reputation points to post images. – user3104453 Dec 15 '13 at 14:46
  • You said: _"I have made 5 strings with different texts"_? Where is it stored.. in a array or in a list or in a file? – Amulya Khare Dec 15 '13 at 14:49
  • In the strings.xml I added 5 strings. They are normal strings, nothing with array or something. – user3104453 Dec 15 '13 at 14:50
  • @user3104453 if your problem is resolved please pick the most helpful answer. This is good practice. – Amulya Khare Dec 30 '13 at 17:19

3 Answers3

2

You have 5 strings in strings.xml. Each of them have an id. You should create an array of these ids (example: string1, string2 etc..) as follows:

private int[] stringIds = {R.string.string1, R.string.string2, R.string.string3, R.string.string4, R.string.string5};

Then use the following code:

private Random rand = new Random();
private int[] stringIds = {R.string.string1, R.string.string2, R.string.string3, R.string.string4, R.string.string5};
public void OnButtonClickChangeTextOfTextView(View view)
{
    TextView textView = (TextView)findViewById(R.id.txt_example1);
    int randomNumber = rand.nextInt(5);
    textView.setText(getResources().getString(stringIds[randomNumber]));
}
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
1

Let's say you have a button defined in xml like this:

<Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="randomNumber" />

And if in this case you have the string defined in a xml file you would make a reference to it in this way ( in this example ill call them R.string.name1 etc...)

 private int[] ids = {R.string.name1 , R.string.name2 , R.string.name3};

 public void randomNumber(View view){  

 //Now let's say you want a random number between 1 and 10.

    int number = new Random().nextInt(3);      

    String randomString = getResource().getString(ids[number]);

    //Then you set the text, let's say to a textView

    textView.setText(randomString);

  }
Luis Pena
  • 4,132
  • 2
  • 15
  • 23
0

You can define a minim and a maximum value. Say you have 5 string your minimum will be 1 while maximum 5.

Looking at this answer: How do I generate random integers within a specific range in Java?

You can learn how to generate a random number that has a range. Then make a if else to check what random number was given: Here is the pseudo:

onClickButton{
random = Math.random etc..
button = findMyButton ( button1 )
button.setText(firstString) if random == 1
button.setText(secondString) if random == 2
}
Community
  • 1
  • 1
Bula
  • 2,398
  • 5
  • 28
  • 54