How to assign ARGB format (e.g. 0xFFFF0000) value to Kotlin INT


In Android, RED (in Java) is defined as

@ColorInt public static final int RED         = 0xFFFF0000;

When we assign to the value as below (in Kotlin), all good.

val value: Int = Color.RED 

However when I replace Color.RED with 0xFFFF0000;

val value: Int = 0xFFFF0000

It will complaint The integer literal does not conform to the expected type Int. How could I workaround it that I could still write in the ARGB format?


I know if we converted to signed Int as below, all works

val value: Int = -0x10000

But that will be too hard for me to recognize the color format value (i.e the FFFF0000 is referring to ARGB format).

asked on Stack Overflow Apr 7, 2019 by Elye

2 Answers

val value: Int = 0xFFFF0000.toInt()

Refer to

answered on Stack Overflow Apr 7, 2019 by farhanjk

I suggest you use Color.parseColor:

val color: Int = Color.parseColor("FFFF0000")

answered on Stack Overflow Apr 7, 2019 by Hamed Momeni

User contributions licensed under CC BY-SA 3.0