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,171
  • 9
  • 39
  • 65

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
  • 25,851
  • 7
  • 65
  • 105
0

Use Random:

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

Then:

tv.setTextColor(color);
Volodymyr Kulyk
  • 6,201
  • 3
  • 34
  • 61