How to set text color of a TextView programmatically?

AndroidTextviewBackground Color

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

yourTextView.setTextColor(color);

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

Solution 4 - Android

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:

https://stackoverflow.com/questions/4229372/java-android-string-to-color-conversion

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNobyView Question on Stackoverflow
Solution 1 - Androiduser370305View Answer on Stackoverflow
Solution 2 - AndroidAlikElzin-kilakaView Answer on Stackoverflow
Solution 3 - AndroidJaveView Answer on Stackoverflow
Solution 4 - AndroidMarek SeberaView Answer on Stackoverflow