Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Color Resource

<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.

like image 278
BomberBus Avatar asked Aug 18 '26 23:08

BomberBus


2 Answers

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();
like image 137
Allan Avatar answered Aug 21 '26 14:08

Allan


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

like image 20
Xavier Falempin Avatar answered Aug 21 '26 13:08

Xavier Falempin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!