0

I'm trying to find a color by its name in an string-array of resources. It is now currently working but this is the last warning in my project and I want to finish him. is there an equivalent in kotlin?

override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val text = spinner.selectedItem.toString()
                textView.text = text
                val textNS = text.replace("\\s".toRegex(), "")
                color = textNS
                val desiredColor = resources.getColor(resources.getIdentifier(textNS, "color", packageName))
                card_view.setCardBackgroundColor(desiredColor)
            }

And This is a sample of the string arrays. I have two because I display the name and the color separately

<string-array name="choosableColors">
        <item name="White">@color/White</item>
        <item name="Ivory">@color/Ivory</item>
        <item name="LightYellow">@color/LightYellow</item>
</string-array>

    <string-array name="choosableColorsNames">
        <item name="White">White</item>
        <item name="Ivory">Ivory</item>
        <item name="LightYellow">Light Yellow</item>
</string-array>

I just want not to have a deprecated function.

Thanks in advance!!

  • Since API level 23, `Resources#getColor(int)` has been deprecated in favor of `Resources#getColor(int, Theme)`. If you're supporting earlier versions, you can use `ContextCompat.getColor(context, resources.getIdentifier(...))` to handle the API level check for you. If not, you can just change that call to `resources.getColor(resources.getIdentifier(...), context.theme)`, or just call `context.getColor(...)` instead. – Mike M. Apr 27 '19 at 05:57
  • 1
    This is exactly what I needed. Thanks! – Edu Sanabria Apr 27 '19 at 06:08

1 Answers1

0

First the array in arrays.xml:

<array name="ingr_color_arr">
      <item>@color/ingr_red1</item>
      <item>@color/ingr_orange1</item>
      <item>@color/ingr_yellow1</item>
      <item>@color/ingr_green1</item>
      <item>@color/ingr_blue1</item>
      <item>@color/ingr_violet1</item>
      <item>@color/ingr_red2</item>
      <item>@color/ingr_orange2</item>
      <item>@color/ingr_yellow2</item>
      <item>@color/ingr_green2</item>
      <item>@color/ingr_blue2</item>
      <item>@color/ingr_violet2</item>
   </array>

Then in color.xml:

<color name="ingr_red1">#FFCC0000</color>
<color name="ingr_orange1">#FFED5F21</color>
<color name="ingr_yellow1">#FFFAE300</color>
<color name="ingr_green1">#FF5B9C0A</color>
<color name="ingr_blue1">#FF0A0D9C</color>
<color name="ingr_violet1">#FF990A9C</color>
<color name="ingr_red2">#FFFFCCCC</color>
<color name="ingr_orange2">#FFFFEACC</color>
<color name="ingr_yellow2">#FFFFFECC</color>
<color name="ingr_green2">#FFC7F5C4</color>
<color name="ingr_blue2">#FFC4DAF4</color>
<color name="ingr_violet2">#FFE1C4F4</color>

Then to use it:

TypedArray ta = res.obtainTypedArray(R.array.ingr_color_arr);
int colorToUse = ta.getResourceId(intGroupNum.intValue() - 1, R.color.recipe_detail_border);
paint.setColor(colorToUse);

The key here is to use getResourceId because setColor(int) is going to expect a resource id for a color. I was getting "Resource not found" errors when I tried getting the value with getIntArray() or getColor().

The most popular answer may work...I didn't try it because I preferred the 'array of colors' design choice better.

Arjun Vyas
  • 409
  • 5
  • 14