简体   繁体   中英

how to transform from string to Color android

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

Try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM