In values / colors.xml
we create the different red colors and create an array to be able to access them later.
<!--Color red-->
<color name="red50">#ffebee</color>
<color name="red100">#ffcdd2</color>
<color name="red200">#ef9a9a</color>
<color name="red300">#e57373</color>
<color name="red400">#ef5350</color>
<color name="red500">#f44336</color>
<color name="red600">#e53935</color>
<color name="red700">#d32f2f</color>
<color name="red800">#c62828</color>
<color name="red900">#b71c1c</color>
<array name="rojos_varios">
<item>@color/red50</item>
<item>@color/red100</item>
<item>@color/red200</item>
<item>@color/red300</item>
<item>@color/red400</item>
<item>@color/red500</item>
<item>@color/red600</item>
<item>@color/red700</item>
<item>@color/red800</item>
<item>@color/red900</item>
</array>
Now to recover that array from code, we would have to do something like this ..
int[] rojosVarios = getResources().getIntArray(R.array.rojos_varios);
Now we would create a random number generator with the range of our array ..
Random rnd = new Random();
int num = rnd.nextInt(rojosVarios.length);
And already it would be to establish that color in a text for example:
texto.setTextColor(rojosVarios[num]);
I leave you a complete example.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="71dp"
android:text="Cambiar Color letra"
android:onClick="cambiarColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btn;
TextView texto;
int[] rojosVarios;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rojosVarios = getResources().getIntArray(R.array.rojos_varios);
texto = (TextView)findViewById(R.id.text);
btn = (Button)findViewById(R.id.button);
}
public void cambiarColor(View view) {
Random rnd = new Random();
int num = rnd.nextInt(rojosVarios.length);
texto.setTextColor(rojosVarios[num]);
}
}
Result: