1
Random myColor = new Random();
tv.setTextColor(Color.rgb(myColor.nextInt(255), myColor.nextInt(255), myColor.nextInt(255)));

string.xml:

<TextView
           android:id="@+id/score"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Score"
           android:textColor="@color/yellow"
/>

This will loop, I want every score text has different color. But its not working

woninana
  • 3,409
  • 9
  • 42
  • 66

2 Answers2

0

You have to get numbers from 0 to 255, so generate a method to get this numbers in order to clean your code:

private int getN() {
    return (int) Math.random() * 255;
}

And the set the randomized color to your tv...

tv.setTextColor(Color.rgb(getN(), getN(), getN()));
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

Use Random:

Random rand = new Random();
Color color = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());

Then:

tv.setTextColor(color);
Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63