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...
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
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:
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) ?
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
textView.setTextColor(Color.parseColor("#0aad3f"))
You can try this to set color of your text programmatically.
textview.textColor=Color.parseColor("#22aadd")
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
textview.setTextColor("#ffffff".toColor())
toColor() in an extension function defined below.
fun String.toColor(): Int = Color.parseColor(this)
for changing the text color, you can follow this -
textView.setTextColor(ContextCompat.getColor(applicationContext,R.color.colorAccent))
User contributions licensed under CC BY-SA 3.0