Search code examples
androidarrayskotlinandroid-background

Pick a color from a list, array not found in Android


In the app, I am developing, I have to choose a color based on a number from 0 to 4. I have decided to create an int-array in the colors.xml but it's not working. The kotlin code, is not able to access the array.

Colors.xml

    <color name="col_0">#8cb6ff</color>
    <color name="col_1">#faf3a2</color>
    <color name="col_2">#52b030</color>
    <color name="col_3">#44af22</color>
    <color name="col_4">#37ad15</color>

    <array name="listColors">
        <item>@color/col_0</item>
        <item>@color/col_1</item>
        <item>@color/col_2</item>
        <item>@color/col_3</item>
        <item>@color/col_4</item>
    </array>


I tried with <integer-array> and same result.

This is how I am access it:

    val colors = context.resources.getIntArray(R.array.listColors)
    setBackgroundColor(colors[value])

I keep getting an unresolved reference for listColors

Any idea ?


Solution

  • The following code working in fragments

    val colors = context?.resources?.getIntArray(R.array.listColors)
    

    Problem may be in the R . Check the imports correctly given R path