79

How can I save color values inside array.xml and retrieve it back to my code as a Color[] array?

1
  • What is this fabled "array.xml"? Where does it live? Commented May 11, 2022 at 23:12

9 Answers 9

188

Define your color resources, then add them to an array for access.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="bright_pink">#FF007F</color>
    <color name="red">#FF0000</color>
    <color name="orange">#FF7F00</color>
    <color name="yellow">#FFFF00</color>
    <color name="chartreuse">#7FFF00</color>
    <color name="green">#00FF00</color>
    <color name="spring_green">#00FF7F</color>
    <color name="cyan">#00FFFF</color>
    <color name="azure">#007FFF</color>
    <color name="blue">#0000FF</color>
    <color name="violet">#7F00FF</color>
    <color name="magenta">#FF00FF</color>

    <array name="rainbow">
        <item>@color/bright_pink</item>
        <item>@color/red</item>
        <item>@color/orange</item>
        <item>@color/yellow</item>
        <item>@color/chartreuse</item>
        <item>@color/green</item>
        <item>@color/spring_green</item>
        <item>@color/cyan</item>
        <item>@color/azure</item>
        <item>@color/blue</item>
        <item>@color/violet</item>
        <item>@color/magenta</item>
    </array>
</resources>

Then access them like this:

int[] rainbow = context.getResources().getIntArray(R.array.rainbow);

for (int i = 0; i < tileColumns; i++) {
    paint.setColor(rainbow[i]);
    // Do something with the paint.
}
Sign up to request clarification or add additional context in comments.

Comments

55

If this is in array.xml:

<resources>
    <array name="colors">
        <item>#ffffff</item>
        <item>#000000</item>
    </array>
</resources>

This will give you the color values for that array:

TypedArray ta = context.getResources().obtainTypedArray(R.array.colors);
int[] colors = new int[ta.length()];
for (int i = 0; i < ta.length(); i++) {
    colors[i] = ta.getColor(i, 0);
}
ta.recycle();

This just expands on the TypedArray example in the documentation: Typed array

1 Comment

You can even reference color resources as items in the array, e.g. <array name="colors"> <item>@color/myred</item> <item>@color/myblue</item> </array>
26

colors.xml

<resources>
    <string-array name="colors">        
        <item>#ff0000</item>
        <item>#00ff00</item>  
        <item>#0000ff</item>
    </string-array>
</resources>

Code in activity class.

String[] allColors = context.getResources().getStringArray(R.array.colors);

Color.parseColor(allColors[0]) // red
Color.parseColor(allColors[1]) // green
Color.parseColor(allColors[2]) // blue

1 Comment

Thanks. I found this a really nice and clean solution. Especially because it shows little redundancy if you don't need a specific name for every color in the list.
12

I can't post a comment, so I must put this in as a new response. I completely agree with Sky Kelsey w.r.t. design choice of using color resource type. However, I found the suggest method to access them did not work. This is the way I implemented the use of an XML array to easily loop through a list of colors and apply the colors to various (Custom painted) views.

First the array in arrays.xml:

    <array name="ingr_color_arr">
      <item>@color/ingr_red1</item>
      <item>@color/ingr_orange1</item>
      <item>@color/ingr_yellow1</item>
      <item>@color/ingr_green1</item>
      <item>@color/ingr_blue1</item>
      <item>@color/ingr_violet1</item>
      <item>@color/ingr_red2</item>
      <item>@color/ingr_orange2</item>
      <item>@color/ingr_yellow2</item>
      <item>@color/ingr_green2</item>
      <item>@color/ingr_blue2</item>
      <item>@color/ingr_violet2</item>
   </array>

Then in color.xml:

<color name="ingr_red1">#FFCC0000</color>
<color name="ingr_orange1">#FFED5F21</color>
<color name="ingr_yellow1">#FFFAE300</color>
<color name="ingr_green1">#FF5B9C0A</color>
<color name="ingr_blue1">#FF0A0D9C</color>
<color name="ingr_violet1">#FF990A9C</color>
<color name="ingr_red2">#FFFFCCCC</color>
<color name="ingr_orange2">#FFFFEACC</color>
<color name="ingr_yellow2">#FFFFFECC</color>
<color name="ingr_green2">#FFC7F5C4</color>
<color name="ingr_blue2">#FFC4DAF4</color>
<color name="ingr_violet2">#FFE1C4F4</color>

Then to use it:

TypedArray ta = res.obtainTypedArray(R.array.ingr_color_arr);
int colorToUse = ta.getResourceId(intGroupNum.intValue() - 1, R.color.recipe_detail_border);
paint.setColor(colorToUse);

The key here is to use getResourceId because setColor(int) is going to expect a resource id for a color. I was getting "Resource not found" errors when I tried getting the value with getIntArray() or getColor().

The most popular answer may work...I didn't try it because I preferred the 'array of colors' design choice better.

1 Comment

I got ""Resource not found" error as well. Your solution worked for me.. Thanks
3

In Kotlin that will be much simpler

 val colors = resources.obtainTypedArray(R.array.colors).use { ta ->
     IntArray(ta.length()) { ta.getColor(it, 0) }
 }

2 Comments

Why is it much simpler? What is the gist of it?
@PeterMortensen, he's talking about syntax compared to Java here: stackoverflow.com/a/10678295/537694 which is simpler in term of code lines
2

If you use integer-array, you can directly grab the color:

File colors.xml

<color name="easy">#FF0000</color>
<color name="normal">#00FF00</color>
<color name="hard">#0000FF</color>

<integer-array name="colors_difficulty">
    <item>@color/easy</item>
    <item>@color/normal</item>
    <item>@color/hard</item>
</integer-array>

Code

val colorsDif = resources.getIntArray(R.array.colors_dificulty);
view.setBackgroundColor(colorsDif[0])

Comments

0

Color.xml:

 <string-array name="listcolors">
        <item>#448AFF</item>
        <item>#FFC107</item>
        <item>#009688</item>
        <item>#ff8000</item>
        <item>#ffbf00</item>
        <item>#0000ff</item>
        <item>#936c6c</item>
        <item>#7733ff</item>
        <item>#7733ff</item>
        <item>#ff8000</item>
        <item>#448AFF</item>
        <item>#0000ff</item>
    </string-array>

activity.java file

Context context;
 String[] colors = context.getResources().getStringArray(R.array.listcolors);

 String bg_color = colors[i]; //i=1,2,3...

Comments

0
<color name="gblue">#4285F4</color>
<color name="ggreen">#34A853</color>
<color name="gyellow">#FBBC05</color>
<color name="gred">#EA4335</color>

<array name="google_colors">
    <item>@color/gblue</item>
    <item>@color/ggreen</item>
    <item>@color/gyellow</item>
    <item>@color/gred</item>
</array>

Use this in Java, Kotlin, or style. Do not use in XML.

2 Comments

What do you mean by "style"? CSS? Or something else?
STYLE -> style.xml in the res folder of the android project.
0

You can also have a nice Kotlin extension function:

fun Resources.getColorsArray(@ArrayRes arrayResId: Int): IntArray {
    obtainTypedArray(arrayResId).use { typedArray ->
        val length = typedArray.length()
        val colors = IntArray(length)
        for (i in 0 until length)
            colors[i] = typedArray.getColor(i, 0)
        return colors
    }
}

Usage:

    <array name="presets_colors">
        <item>#FF0000ff</item>
        <item>#FF00ff00</item>
        <item>#FF00ffff</item>
        <item>#FFff0000</item>
        <item>#FFff00ff</item>
        <item>#FFffff00</item>
    </array>
val presetsColors = resources.getColorsArray(R.array.presets_colors)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.