Google+

Android4Beginners.com

Apps development for Android step by step

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.

This is a code example:

What’s very convenient in Android Studio it shows colors on the left margin next to the code. Of course you have also live preview for layout created in XML files.

What’s very convenient in Android Studio it shows colors on the left margin next to the code. Of course you have also live preview for layout created in XML files.

Instead of putting RGB values into a code you could refer to color resources.

Of course first you have to add color description to colors.xml file.

In colors.xml file you could give your own names to colors and then use them in XML or Java code.

In colors.xml file you could give your own names to colors and then use them in XML or Java code.

In Java you have to find TextView (as described in previous lesson) and then use setTextColor() method. As an argument we give a color, but we could do it in many different ways (all described in the appendix).

This is a sample code.

To see the effect we have to use emulator or real device.

Remember that referring to colors.xml resources in Java you have to use getResources().getColor(R.color.your_color_name).

We could set colors in Android Java in similar way as in XML, but there are much more options how to decode RGB values (Android Studio)

We could set colors in Android Java in similar way as in XML, but there are much more options how to decode RGB values (Android Studio)

Step 2. Set a background color in XML and Java – android:background and setBackgroundColor ()

There isn’t any dedicated XML tag attribute for just color of background. But we could use android:background. It allows to use as a background various things, like a graphic, but those options we will practice later.

Now we just focus on colors and this is our XML code with yellow background (this time in #RRGGBB format):

Again we could use colors.xml instead of putting RGB values directly into layout.

And this is combination of text color and background color (Android Studio)

And this is combination of text color and background color (Android Studio)

 In Java all we need is setBackgroundColor() method working in the same way as setTextColor().

This is a code example using Android class Color as a color source (it’s also explained in appendix about colors):

Step 3. Colors for special types of text.

Beside standard text we could modify color of links. If there is a valid link in a string, it could have different color than rest of a text.

First let’s add a link. We use standard HTML <a> tag for it (the syntax is <a href=”URL”>link _description</a>). Add this into strings.xml resources file.

Then in XML we use attribute android:textColorLink:

And in Java method setLinkTextColor():

Beside color resources or Color class we could use all others color references.

We could mark links with color different than text color (Android Virtual Device).

We could mark links with color different than text color (Android Virtual Device).

Another two options would be more useful when we start to input a text (like EditText and SearchView): highlight color and hint color. First changes color of marked text and second sets a color of hint explaining what to do.

Example in XML:

Example in Java:

Step 4. How to check current colors in Android Java?

There is a list of getters for colors that allows uscto check current color values:

getCurrentTextColor()

getCurrentHintTextColor()

getCurrentHintTextColor()

getHighlightColor() – works from API 16 (Android 4.1 Jelly Bean)

Mind that they all return negative int values (not positive hex as we used for colors definition). For instance red is not 00FF00, but -16711936. Why? Because the result is counted from expression: CurrentColor-(WhiteColor+1) all in decimal. In that example red is 16711680 and white is 16777215 so 16711680-(16777215+1) equals -16711936. Remember that you could compare it with predefined constants like Color.RED instead of integer values.

This is a code example:

There are also methods for so called color state lists, like getTextColors() . You should not confuse them with those mention above, as they return a group of colors assigned to different states of View (we would explore it in future).

Summary:  We have learnt how to change text and background color both in XML and Java. Moreover we know hot to check current colors. That’s not yet everything about colors – in the next lesson we try to color separately every word of text.

Previous:
Next: