1

I need to convert an string to Color my api response is this "mainColor": "#8439FF", i need to transformit to Color

i have tried the next solutions

    int color = Color.parseColor(mainColor);

val string = mainColor
val ColorPrimario = string.replaceFirst("^#".toRegex(), "").toInt(16)

the problem whit this solutions is that the result its an int not Color, and it marks error for that reason. any help would be appreciated

-------------Edit-------------

I have solved the problem i did it this way

val string = mainColor
val color = Color(string.toColorInt())
user10625391
  • 177
  • 3
  • 19
  • Does this answer your question? [Color from hex string in jetpack compose](https://stackoverflow.com/questions/60247480/color-from-hex-string-in-jetpack-compose) – Sky Aug 01 '22 at 16:05

1 Answers1

1

Try this:

val color: Color = Color.valueOf(Color.parseColor(mainColor))

See https://developer.android.com/reference/android/graphics/Color#valueOf(int)

Eddie Lopez
  • 1,101
  • 7
  • 15
  • tried this but the editor tells me that valueOf and paseColor are unresolved reference – user10625391 Aug 01 '22 at 16:15
  • @user10625391 You need to import `android.graphics.Color`. This *is* how you do it - parse the string to a colour int, then use that colour int to create a `Color` instance – cactustictacs Aug 01 '22 at 21:15