2

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())
1

1 Answer 1

1

Try this:

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

tried this but the editor tells me that valueOf and paseColor are unresolved reference
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.