Reputation: 37
I have a string array in strings.xml which is an array of color name. And, I listed colors in colors.xml with the same name as the array item. Now, I am trying to set the background of view using the color name (string) from strings.xml. How can i do that?
<color name="aliceBlue">#F0F8FF</color>
<color name="antiqueWhite">#faebd7</color>
<color name="antiqueWhite1">#ffefdb</color>
<color name="antiqueWhite2">#eedfcc</color>
<color name="antiqueWhite3">#cdc0b0</color>
<color name="antiqueWhite4">#8b8378</color>
<color name="aquaMarine1">#7fffd4</color>
<color name="aquaMarine2">#76eec6</color>
<color name="aquaMarine4">#458b74</color>
<color name="azure1">#f0ffff</color>
<color name="azure2">#e0eeee</color>
<color name="azure3">#c1cdcd</color>
<color name="azure4">#838b8b</color>
color.xml
<string-array name="colorNameArray">
<item>AliceBlue</item>
<item>AntiqueWhite</item>
<item>AntiqueWhite1</item>
<item>AntiqueWhite2</item>
<item>AntiqueWhite3</item>
<item>AntiqueWhite4</item>
<item>aquamarine1</item><item>
aquamarine2</item>
<item>aquamarine4</item>
<item>azure1</item>
<item>azure2</item>
<item>azure3</item>
<item>azure4</item>
strings.xml
Please guys, your help is appreciated.
Upvotes: 0
Views: 141
Reputation: 11948
change
<item>AliceBlue</item>
to
<item>@color/AliceBlue</item>
change all of them, you need @color/
before tag
for more info see:
How can I save colors in array.xml and get its back to Color[] array
then you can get your colors as:
int[] colorArray = getResources().getIntArray(R.array.colorNameArray);
then you can set your color.
if you have more that one item and you need change all of them you can use following code:
1- create one Array of View
ArrayList<View> views = new ArrayList<View>();
2- add All view that you want set background to your list
views.add(iv);
views.add(tv);
3- in for statement change all background, like:
for (int i = 0 ; i < views.size() ; i++ )
{
// set background with views.get(i) and colorArray[i]
}
Upvotes: 1
Reputation: 9261
get a java object reference to the view. And call this method on that reference setBackgroundColor(getResources().getColor(R.color.color_name));
Upvotes: 0