How to use "setTextColor(hexaValue)" on Kotlin for Android,



In Java, I could directly change the text color of a TextView, using the standard hexa-decimal value of it:

    textView.setTextColor(0xffffffff); //white
    textView.setTextColor(0x00000000); //transparent
    textView.setTextColor(0xff000000); //black
    textView.setTextColor(0xff0000ff); //blue

Very easy...

The problem

On Kotlin, if I try to write such a thing, I get with a weird build error:

Error:(15, 18) None of the following functions can be called with the arguments supplied: public open fun setTextColor(p0: ColorStateList!): Unit defined in android.widget.TextView public open fun setTextColor(p0: Int): Unit defined in android.widget.TextView

What I've tried

I tried to search about this over the Internet, and I couldn't see anything special about hexa-decimal values. Seems the same like on Java:

Then I decided to just write in Java, and convert to Kotlin. The result is very unreadable in terms of the color value:

    textView.setTextColor(-0x1) //white
    textView.setTextColor(0x00000000) //transparent
    textView.setTextColor(-0x1000000) //black
    textView.setTextColor(-0xffff01) //blue

To me it seem that the hexadecimal value of Integer that is used for Kotlin is signed, while on Java it's converted to signed one automatically, so this causes flipping of values and the need to set a minus sign when needed.

The only thing I can think of, that still allows to read it well, is something like this:


However, this has multiple disadvantages:

  1. It is way longer.
  2. It converts a String, so it's much less efficient
  3. Most importantly: it works only from API 26 (Android O) , which currently is active on about 1% of Android devices worldwide.

The questions

Why does it occur?

What exactly can I do to make it the most readable, without string conversions, and work on all Android versions (minSdkVersion 14 in my case) ?

asked on Stack Overflow Oct 29, 2017 by android developer • edited Oct 29, 2017 by android developer

6 Answers


Oxff000000 is resolved to Long in Kotlin so right now there is no way to use this literal as is, however 0xff000000.toInt() will give you exactly the same result as -0x1000000 so you can use .toInt() approach. Under the hood, it's the equivalent of (int)4278190080L Java cast.

Also, with Kotlin extensions you can write a simple property like that

var TextView.textColor: Long
get() {
    //... not important
set(value: Long) {

and you'll be able to use a more concise syntax textView.textColor = 0xff000000

UPDATE: As of Kotlin 1.3 it will be possible to use concise syntax like that 0xff000000u See: Jetbrains blog and the original proposal

answered on Stack Overflow Nov 7, 2017 by Dmitry Sitnikov • edited Sep 27, 2018 by Dmitry Sitnikov
answered on Stack Overflow Sep 12, 2018 by kiko carisse

You can try this to set color of your text programmatically.

answered on Stack Overflow Nov 3, 2017 by Yogesh Paliyal

Sorry for putting something on such an old question, but an extension function really seemed like the nicest approach:

fun TextView.setTextColor(color: Long) = this.setTextColor(color.toInt())

now, you can just go textView.setTextColor(0xff00ff00) again

answered on Stack Overflow Oct 2, 2019 by Joozd

toColor() in an extension function defined below.

fun String.toColor(): Int = Color.parseColor(this)
answered on Stack Overflow Sep 28, 2018 by Sanket Naik

for changing the text color, you can follow this -

answered on Stack Overflow Dec 10, 2019 by Imtiaz Dipto • edited Dec 10, 2019 by Imtiaz Dipto

User contributions licensed under CC BY-SA 3.0