How can I save color values inside array.xml and retrieve its back to my code as Color [] array?
Thanks beforehand!
Color cBlue = new Color(0,0,255); Color cRed = new Color(255,0,0); ArrayList colors = new ArrayList(); colors. add(cBlue); colors. add(cRed);
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.
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" ).
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. }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With