0

I have a specific question about Android. Using setTextColor,

the developers guide shows void setTextColor (int color). Ok checking stackoverflow syntax is myObjectName.setTextColor(Color.GREEN)

Can someone explain the integer value in setTextColor(int color) and what integer values as they relate to colors?

I know the hex value color code, just don't understand how this relates to the integer value.

  • You could just write a util class that has those corresponding values as INTs but underneath, you have the actual hex? – Eenvincible Jul 11 '16 at 17:37

1 Answers1

1

Android has a Color class... the

android.graphics.Color

and every Color you get when you call

Color.aColor is in the class defined as a constant. Example

enter image description here

so for example:

Color.BLACK is mapped to a integer value, in this case

int BLACK
Constant Value: -16777216 (0xff000000)

and as you can imagine that value in hex is related aswell to a RGB+Alpha value

so at the end:

Color.BLACK = -16777216 = 0xff000000
                             | | | |
                        alpha  | | |
                              Red| |
                              Green|
                                Blue

why an integer? well every color can take a value between 0 and 255 (8 bits exactly) and you can perfectly match
Red Green Blue and Alpha, all those a 8 bits makes 32 bits which can be represented with an integer

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Thanks for answering. I get the hex representation. Just don't understand how the method setTextColor(int color) is expecting an integer input and how it relates that to the color class? – Randall Millican Jul 11 '16 at 17:51
  • This explanation helped me so much, THANKS! – David Nov 03 '20 at 04:24