Home > Software design >  how to transform from string to Color android
how to transform from string to Color android

Time:08-02

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

CodePudding user response:

Try this:

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

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

CodePudding user response:

You can try this:

val mainColor = "#8439FF"
val colorPrimario = mainColor.toColorInt().toColor()

and

Modifier.background(color = Color(colorPrimario.toArgb()))
  • Related