3
<resources> <color name="red">#e51c23</color> <color name="pink">#e91e63</color> <color name="purple">#9c27b0</color> <color name="deep_purple">#673ab7</color> <string-array name="colors_hex_code"> <item>@color/red</item> <item>@color/pink</item> <item>@color/purple</item> <item>@color/deep_purple</item> </string-array> </resources> 

Hello, I declared colors.xml like about code and when I access this value form java like

String[] s = getResources().getStringArray(R.array.colors_hex_code); Toast.makeText(getActivity(), "First Color: " + s[0], Toast.LENGTH_SHORT).show(); 

, why s[index] always return null? I would like to get hex color codes from "colors_hex_code" string array. Is it possible to access like this? Please help. Thank u.

1
  • Because it is not valid.. you can't define colors in string-array Commented Oct 14, 2014 at 8:19

6 Answers 6

6

Change string-array to integer-array:

<resources> <color name="red">#e51c23</color> <color name="pink">#e91e63</color> <color name="purple">#9c27b0</color> <color name="deep_purple">#673ab7</color> <integer-array name="colors_hex_code"> <item>@color/red</item> <item>@color/pink</item> <item>@color/purple</item> <item>@color/deep_purple</item> </integer-array> </resources> 

And java code:

int[] s = getResources().getIntArray(R.array.colors_hex_code); Toast.makeText(getActivity(), "First Color: " + String.format("#%06X", (0xFFFFFF & s[0])), Toast.LENGTH_SHORT).show(); 
Sign up to request clarification or add additional context in comments.

Comments

2

You probably should not declare a "string-array" if you store colors and not string inside it.

Try to take a look at this post :

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

Comments

0

From the documentation of StringArray

<item> A string, which can include styling tags. The value can be a reference to another string resource 

So if you want to have a string-array you have to declare the colours in strings.xml, or you

Comments

0

Replace

<string-array name="colors_hex_code"> <item>@color/red</item> <item>@color/pink</item> <item>@color/purple</item> <item>@color/deep_purple</item> </string-array> 

with

<string-array name="colors_hex_code"> <item>#FFEA4E3C</item> <item>#FFF67A81</item> <item>#FF665EC7</item> <item>#FF3B3673</item> </string-array> 

Comments

0

Use array instead of string-array

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#e51c23</color> <color name="pink">#e91e63</color> <color name="purple">#9c27b0</color> <color name="deep_purple">#673ab7</color> <array name="colors_hex_code"> <item>@color/red</item> <item>@color/pink</item> <item>@color/purple</item> <item>@color/deep_purple</item> </array> </resources> 

and get color using

int[] s = context.getResources().getIntArray(R.array.colors_hex_code); // It will show you a color code value Toast.makeText(getActivity(), "First Color: " + s[0], Toast.LENGTH_SHORT).show(); 

Comments

-1

Create a colour.xml file

Add some colours :

<color name="white">#ffffff</color> <color name="black">#000000</color> 

Then in the activity :

textView.setTextColor(R.color.white); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.