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

4

Background

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
    //etc...

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:

https://kotlinlang.org/docs/reference/basic-types.html

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:

textView.setTextColor(Integer.parseUnsignedInt("ffff0000",16));

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

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

5 Answers

5

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) {
    this.setTextColor(value.toInt())
}

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
5
textView.setTextColor(Color.parseColor("#0aad3f"))
answered on Stack Overflow Sep 12, 2018 by kiko carisse
1

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

textview.textColor=Color.parseColor("#22aadd")
answered on Stack Overflow Nov 3, 2017 by Yogesh Paliyal
1
textview.setTextColor("#ffffff".toColor())

toColor() in an extension function defined below.

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

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

User contributions licensed under CC BY-SA 3.0