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:
- It is way longer.
- It converts a String, so it's much less efficient
- 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) ?