Lesson 1.4: How to change a color of text and background in TextView?

From the previous lesson we know how to display a text on Android device’s screens both using XML layout files and Java code. Now it’s time to format that text. We start with font and background colors, but beside a standard text color there are also other options, like links, highlight and hint colors. Moreover we would check current colors.

Colors in Android are defined in RGB format. Colors defined by user could be stored in colors.xml resource file. Read our Everything about colors in Android appendix to understand how to define colors. In this lesson we just focus on using colors, not defining them.

Step 1. Set a text color in XML and Java – android:textColor and setTextColor()

All we have to do to set text color in XML is to add one more attribute called android:textColor to TextView tag. As its value we could put #RGB, #ARGB, #RRGGBB, #AARRGGBB color value or reference to color saved in colors.xml (all is explained in the appendix). For instance RGB red color value is #F00.

Comments