Can the elements of values / color.xml be traversed in android?

1

I would like to know if it is possible to access the elements declared within values / color with a wildcard, I mean ...

If I want to randomly display any of the colors I have declared in my colors.xml, how could I access them without putting "n" amount of ifs?

<color name="colorPrimary">#384046</color>
<color name="colorPrimaryDark">#384046</color>
<color name="colorAccent">#384046</color>
<color name="white">#FFFFFF</color>

<!--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>
    
asked by Mike Guijarro 01.07.2018 в 09:07
source

2 answers

1

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:

    
answered by 01.07.2018 в 10:20
-1
  

I would like to know if it is possible to access the declared elements   within values / color with a wildcard, I mean ...

No, access to the element must be done through the id of the element.

If you want to obtain a random color, you must perform a method to obtain a random value of these colors, in which you must define all the ids, for example the following method which obtains a whole value of the color.

public int getRandomColor() {
    //Define el array de colores almacenados en colors.xml.
    int[] colores = {R.color.red50, R.color.red100, R.color.red200, R.color.red300, R.color.red400, R.color.red500, R.color.red600, R.color.red700, R.color.red800, R.color.red900};
    Random rnd = new Random();
    int num = rnd.nextInt(colores.length);
    return getResources().getColor(colores[num]);
}

The previous method you can use to get the color and use it in several methods like setColor ( getRandomColor () ) , setBackgroundColor ( getRandomColor () ) or setTextColor ( getRandomColor () )

Example:

    
answered by 02.07.2018 в 18:37