<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.
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();
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
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