-1

I am generating random flashes of a button and I want to know what exact number is being chosen each time so I can eventually add them to an ArrayList for a project I am undergoing.

TextView randomTextView;
Random r = new Random();

private void sequenceFunction() {
    //Change Alpha from Fully Visible to Invisible
    final Animation animation = new AlphaAnimation(1, 0);
    //Duration - A Second
    animation.setDuration(1000);
    //Animation Rate
    animation.setInterpolator(new LinearInterpolator());
    animation.setStartOffset(250);
    animation.setDuration(250);
    //Repeat Animation
    animation.setRepeatCount(r.nextInt(10));

    // Reverse animation at the end so the button will fade back in
    animation.setRepeatMode(Animation.REVERSE);

    //Button 1 Flashes
    final Button btn = (Button) findViewById(R.id.button);
    btn.startAnimation(animation);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            view.clearAnimation();
        }
    });
}

I want to display the result of what ever random number has been generated via the randomTextView TextView. This part is critical so that I know that the random function is working as it is supposed to. I have already tried

randomTextView.setText(r.nextInt(10));

However it didn't like it. Any ideas on how to get the random number selected would be greatly appreciated ?

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Leo_D
  • 27
  • 4

3 Answers3

1

Hope this helps -

private void sequenceFunction() {
    //Change Alpha from Fully Visible to Invisible
    final Animation animation = new AlphaAnimation(1, 0);
    //Duration - A Second
    animation.setDuration(1000);
    //Animation Rate
    animation.setInterpolator(new LinearInterpolator());
    animation.setStartOffset(250);
    //Repeat Animation
    int randomValue = r.nextInt();
    // code to add value to array
    animation.setRepeatCount(randomValue);
    randomTextView.setText(String.valueOf(randomValue));

    // Reverse animation at the end so the button will fade back in
    animation.setRepeatMode(Animation.REVERSE);

    //Button 1 Flashes
    final Button btn = (Button) findViewById(R.id.button);
    btn.startAnimation(animation);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            view.clearAnimation();
        }
    });


}
Anindita Pani
  • 544
  • 3
  • 6
0

If you have look in the documentation of TextView, you can see that setText(int) actually expects a resource id: https://developer.android.com/reference/android/widget/TextView.html#setText(int)

So you have to turn your int to a CharSequence, you could simply do

randomTextView.setText("" + r.nextInt(10));

which converts your integer to a string.

SimMac
  • 479
  • 5
  • 16
  • Hi Thanks for your reply how would I get the number of animation.setRepeatCount(r.nextInt(10)); as when I do the randomTextView.setText(r.nextInt(10)+""); it sees it as a new random number and not the one I am using to generate the animation flashes ? – Leo_D Jan 11 '17 at 18:35
  • In this case, just store the value in a variable like this: `int randomNumber = r.nextInt(10);` `animation.setRepeatCount(randomNumber);` `...` `randomTextView.setText(Integer.toString(random));` – SimMac Jan 11 '17 at 18:40
  • save `r.nextint(10)` in some variable then pass it to `animation.setRepeatCount()` and use it anywhere. – jack jay Jan 11 '17 at 18:41
  • @SimMac Please refer to [this answer](http://stackoverflow.com/a/4105406/2948765) for an explanation why `"" + ...` is bad style. – Tobias Jan 11 '17 at 19:26
0

TextView.setText can be used with either a resource ID (which is an integer) or with a CharSequence, for example a String.

There are many ways to do this, including

int random = r.nextInt(10);
randomTextView.setText(String.valueOf(random);
randomTextView.setText(Integer.toString(random);
randomTextView.setText(String.format("%d", random);

Do not use "" + r.nextInt(10). I know, it is short, it is handy, but it is simply inefficient and bad style.

Tobias
  • 7,723
  • 1
  • 27
  • 44
  • Hi thanks for the recorrection however how would I use this to get the exact random number that is being generated by animation.setRepeatCount(r.nextInt(10)); ? – Leo_D Jan 11 '17 at 18:39
  • This code displays the generated number in the `TextView`. If you want to use the number multiple times, you will need to cache it in a variable like this: `int number = r.nextInt(10); randomTextView.setText(String.valueOf(number));` and then use the variable `number`. – Tobias Jan 11 '17 at 18:41