Search code examples
javaandroidandroid-resources

Converting xml integer-array of colors to String Array returns null values - How to convert it to Hex programmatically?


I'm trying to convert this list of colors from my colors.xml to a Java String Array

<color name="redditred">#EA0027</color>
    <color name="redditorange">#FF8717</color>
    <color name="redditlime">#94E044</color>
    <color name="redditmint">#0DD3BB</color>
    <color name="redditblue">#24A0ED</color>
    <color name="redditpink">#FF66AC</color>
    <color name="redditgold">#DDBD37</color>
    <color name="redditgrey">#A5A4A4</color>
    <color name="redditsemiblack">#222222</color>
    
    <integer-array name="redditColors">
        <item>@color/redditred</item>
        <item>@color/redditorange</item>
        <item>@color/redditlime</item>
        <item>@color/redditmint</item>
        <item>@color/redditblue</item>
        <item>@color/redditpink</item>
        <item>@color/redditgold</item>
        <item>@color/redditgrey</item>
        <item>@color/redditsemiblack</item>
    </integer-array>

I've tried using getResources and then getting the array from the xml resource file and printing it. It returns the length of 17 as expected. However when I print the hex values using Arrays.toString(), it gives me an array of null values. What am I doing incorrectly, and how can I get my code to print the Hex colors? eg: [#EA0027, #FF8717, #94E044, ...... ]

String[] colors = getResources().getStringArray(R.array.redditColors);
Log.d(TAG, "onCreate: " + colors.length);
Log.d(TAG, "onCreate: " + Arrays.toString(colors));

Output:

onCreate: 17
onCreate: [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]

Edit:

String[] colors = getResources().getStringArray(R.array.redditColors);
for(int i = 0; i < colors.length; i++){
   String hexInString = String.format("#%06X", (0xFFFFFF & colors[i]));
   Log.d(TAG, "onCreate: hex: " + hexInString);
}

Solution

  • You should use getIntArray() instead of getStringArray() and convert to Hex with Integer.toHexString()

    int[] colors = getResources().getIntArray(R.array.redditColors);
    
    for (int color : colors) {
        String ColorString = "#" + Integer.toHexString(color);
        Log.d("LOG_TAG", "Hex Color: " + ColorString);
    }
    

    Result:

    2021-01-04 08:22:11.819 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ffea0027
    2021-01-04 08:22:11.819 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ffff8717
    2021-01-04 08:22:11.820 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ff94e044
    2021-01-04 08:22:11.820 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ff0dd3bb
    2021-01-04 08:22:11.820 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ff24a0ed
    2021-01-04 08:22:11.820 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ffff66ac
    2021-01-04 08:22:11.820 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ffddbd37
    2021-01-04 08:22:11.820 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ffa5a4a4
    2021-01-04 08:22:11.820 32308-32308/com.example.... D/LOG_TAG: Hex Color: #ff222222