348

How can I set the text color of a TextView to #bdbdbd programatically?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Noby
  • 6,562
  • 9
  • 40
  • 63
  • A note about tweaking UI in code, please consider the advantages of seeing the UI in design time, minimizing the runtime changes to minimum. – AlikElzin-kilaka May 18 '16 at 05:04
  • Use `textView.setTextColor(textView.getContext().getColor(R.color.white));` to set the text to white or any other color specified in the colors.xml – Ola Ström Mar 01 '22 at 14:00

4 Answers4

852

Use,..

Color.parseColor("#bdbdbd");

like,

mTextView.setTextColor(Color.parseColor("#bdbdbd"));

Or if you have defined color code in resource's color.xml file than

(From API >= 23)

mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));

(For API < 23)

mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));
user370305
  • 108,599
  • 23
  • 164
  • 151
260

Great answers. Adding one that loads the color from an Android resources xml but still sets it programmatically:

textView.setTextColor(getResources().getColor(R.color.some_color));

Please note that from API 23, getResources().getColor() is deprecated. Use instead:

textView.setTextColor(ContextCompat.getColor(context, R.color.some_color));

where the required color is defined in an xml as:

<resources>
  <color name="some_color">#bdbdbd</color>
</resources>

Update:

This method was deprecated in API level 23. Use getColor(int, Theme) instead.

Check this.

Community
  • 1
  • 1
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • Thanks, I was having trouble for some reason setting the holo colors, like `tv.setTextColor(color.holo_green_light)`. The text was showing up invisible. So I set the color as a resource in XML like `@android:color/holo_green_light` and set it programmatically with that. – RTF Feb 17 '14 at 12:32
  • 8
    from API 23 get.Color() is deprecated. use instead ContextCompat.getColor(context, R.color.color_name) – Rami Mar 18 '16 at 15:25
39
yourTextView.setTextColor(color);

Or, in your case: yourTextView.setTextColor(0xffbdbdbd);

Jave
  • 31,598
  • 14
  • 77
  • 90
  • 1
    what does `0xff` mean ? and whats the diff bet `setTextColor(0xffbdbdbd)` and `setTextColor(Color.parseColor("#bdbdbd"))` ? – mrid Jul 22 '17 at 10:26
  • 2
    0xFFBDBDBD is the hexadecimal reprecentation of an integer, where the different parts represent the amounts of alpha, red, green and blue (0xAARRGGBB) (the 0xff means the alpha is completely white - that is, no transparency). "#bdbdbd" is a string that is parsed to the same integer when passed into the function `parseColor`. – Jave Jul 23 '17 at 10:55
  • when you have 4 bytes to define a color, the first byte refers to alpha channel in color, meaning transparency of the color, the remaining bytes refers to red, green and blue; so when you have 0xffbdbdbd is aRGB format and without the first 2 ff is RGB format. Also when you do Color.parseColor("#bdbdbd") this will transform the hexaddecimal formatted string '#bdbdbd' into an integer which will be equivalent to 0xbdbdbd (hex) => 12434877 (dec). – alexscmar Dec 25 '19 at 13:40
28
TextView tt;
int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
tt.setTextColor(color);

also

tt.setBackgroundColor(Integer.parseInt("d4d446", 16)+0xFF000000);

also

tt.setBackgroundColor(Color.parseColor("#d4d446"));

see:

Java/Android String to Color conversion

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244