Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save colors in array.xml and get its back to Color[] array

Tags:

android

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

Thanks beforehand!

like image 806
Victor Bogoslavsky Avatar asked Feb 02 '12 15:02

Victor Bogoslavsky


People also ask

How do you add color to an ArrayList?

Color cBlue = new Color(0,0,255); Color cRed = new Color(255,0,0); ArrayList colors = new ArrayList(); colors. add(cBlue); colors. add(cRed);

What is array in color?

To define the color or shading of object displayed on screen or printed on paper, color values are used, which are typically based on color models. Models may include a single color (such as for the grayscale model) or multiple colors.

What is XML color?

A color value defined in XML. The color is specified with an RGB value and alpha channel. You can use a color resource any place that accepts a hexadecimal color value. You can also use a color resource when a drawable resource is expected in XML (for example, android:drawable="@color/green" ).


1 Answers

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. } 
like image 197
Sky Kelsey Avatar answered Oct 06 '22 11:10

Sky Kelsey