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:

XHTML

1

2

3

4

5

6

7

<TextView

android:id="@+id/this_is_id_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/my_best_text"

android:textColor="#F00"

/>

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.

XHTML

1

android:textColor="@color/blue"

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 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.

Java

1

2

3

4

5

6

7

8

@Override

protectedvoidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView textElement=(TextView)findViewById(R.id.this_is_id_name);

textElement.setTextColor(0xFF00FF00);//this is green color

}

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)

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

XHTML

1

2

3

4

5

6

7

8

<TextView

android:id="@+id/this_is_id_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/my_best_text"

android:textColor="@color/blue"

android:background="#FFFF00"

/>

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)

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

Java

1

textElement.setBackgroundColor(Color.MAGENTA);

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.

XHTML

1

<string name="link_text">This is <a href="http://www.android4beginners.com">a link</a> in a text</string>

Then in XML we use attribute android:textColorLink:

XHTML

1

android:textColorLink="@color/color_link"

And in Java method setLinkTextColor():

Java

1

textElement.setLinkTextColor(Color.GREEN);

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

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:

XHTML

1

2

3

android:textColorHighlight="@color/color_highlight "

android:textColorHint="@color/color_hint "

Example in Java:

Java

1

2

3

textElement.setHighlightColor(Color.BLUE);

textElement.setHintTextColor(Color.GRAY);

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:

Java

1

intcolor=textElement.getCurrentTextColor();

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.

What & Why

There are many Android tutorials for developers, but it is difficult to find easy to understand lessons for beginners. Android4Beginners.com explain how to build your apps step by step with clear instructions, examples and illustrations. Feel free to share your tips & tricks too!