Assigning Labels

The simplest widget is the label, referred to in Android as a TextView. Like in most GUI toolkits, labels are bits of text not editable directly by users. Typically, they are used to identify adjacent widgets (e.g., a "Name:" label before a field where one fills in a name).

In Java, you can create a label by creating a TextView instance. More commonly, though, you will create labels in XML layout files by adding a TextView element to the layout, with an android:text property to set the value of the label itself. If you need to swap labels based on certain criteria, such as internationalization, you may wish to use a resource reference in the XML instead, as will be described in Chapter 9. TextView has numerous other properties of relevance for labels, such as:

• android:typeface to set the typeface to use for the label (e.g., monospace)

• android:textStyle to indicate that the typeface should be made bold (bold), italic (italic), or bold and italic (bold italic)

• android:textColor to set the color of the label's text, in RGB hex format (e.g., #FF0000 for red)

For example, in the Basic/Label project, you will find the following layout file: <?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout width="fill parent" android:layout height="wrap content" android:text="You were expecting something profound?" />

As you can see in Figure 6-1, just that layout alone, with the stub Java source provided by Android's project builder (e.g., activityCreator), gives you the application.

LabelDemo

You mere expecting something profound?

Figure 6-1. The LabelDemo sample application

0 0

Post a comment