I cannot post a comment, so I have to add this as a new answer. I completely agree with Sky Kelsey wrt design choice using the type of resource color. However, I found that the proposed method of accessing them does not work. Thatβs how I implemented using an XML array to easily scroll through the list of colors and apply colors to various (individually colored) representations.
First an 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) expects the resource identifier for the color. I was getting "Resource not found" errors when I tried to get the value using getIntArray() or getColor() .
The most popular answer may work ... I have not tried, because I preferred to choose the design of the "array of colors".
user2229491 Aug 15 '13 at 1:11 2013-08-15 01:11
source share