Text Manipulation

In views that incorporate text, such as TextView, EditText, and Button, the text is represented in the XML layout file by the android:text element. As discussed in the beginning of this chapter, it is good practice to initialize this with a string defined in the strings

XML file, so that all strings are contained in a single place. Therefore, a way to add text to an UI element, such as TextView, looks like the following:

<TextView android:text="@string/myTextString"

android:id="@+id/my_text_label" android:layout_width="wrap_content" android:layout_height="wrap_content" />

The default font depends on Android device and user preferences. To specify the exact font, use the elements shown in Table 4.3.

Table 4.3 Useful TextView Attributes with Default Values in Bold in the Last Column

Possible and

TextView Attribute

XML Element

Java Method

Default Values

Display string

android:text

setText(CharSequence)

Any string

Font size

android:textSize

setTextSize(float)

Any dimension

Font color

android:textColor

setTextColor(int)

Any color

Background color

N/A

setBackgroundColor(int)

Any color

Font style

android:textStyle

setTypeface(Typeface)

bold

italic

bold italic

Font type

android:typeface

setTypeface(Typeface)

normal

sans

serif

monospace

Text placement in

android:gravity

setGravity(int)

top

display area

bottom

left

right

(more...)

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment