Contents
Android Developer Reference
This is the official Google Android site for developers. Here you will find all the documentation you need to develop rich android applications.
You will most likely visit this site often.
All tutorials and sample code are credited to their respected owners.
Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
TextView
This topic covers the android textview widget.
The TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing; see EditText for a subclass that configures the text view for editing.
The Android SDK includes a simple text control for use within your layouts: TextView (android.widget.TextView). A good example of TextView control usage would be to display textual labels for other controls, like "Enter Date:", "Enter Name:" or "Enter a Password:" etc...
A sample layout file is show below with the TextView widget code in place.
To configure how the TextView control looks and behaves, you can adjust the control's attributes by selecting the control (either in the Component Tree window or the Preview window) and changing its attributes, as shown on the Properties Tab. You can also edit the XML file directly if you desire.
Specific attributes of TextView widgets you will want to be aware of:
- Give the TextView control a unique name using the id property.
- Set the text of the TextView using the text property.
- Set the layout_width and the layout_height to the appropriate values.
- Set the font, color, size, and width as desired.
Some of the most common properties of the TextView are:
- android:background="#0000ff" - Used to set the background of the TextView. This can be a hex color value, or a drawable.
- android:autoLink="all" - By default, any text contents within a TextView control is displayed as plain text. However, by setting one simple attribute called autoLink with "all", you can enable automatic detection of web, email, phone and address information within the text.
-
android:lines="5" - By default, text contents of a TextView control will wrap and continue on multiple lines unless you specify otherwise. You can customize this behavior in a number of ways:
- You can use the android:lines attribute to set the TextView control to a specific number of lines.
- You can use the android:minLines and android:maxLines attribute to set the TextView control to a never grow smaller or larger than specific number of lines.
- You can use the android:singleLine attribute to avoid wrapping onto multiple lines, but instead provide a one line alternative view of the text.
- You can use the android:lineSpacingExtra attribute to specify the distance of whitespace between each line (e.g. double spaced for readability).
- android:textColor="#ff0000" - You can control the color of the text within the TextView control by using the textColor attribute. This attribute can be set to a color resource, or a specific color by hex value.
- android:singleLine="true" - You can use this attribute to avoid wrapping onto multiple lines, but instead provide a one line alternative view of the text.
- android:textStyle="bold" - You can control the style of the text (bold, italic) and font family (sans, serif, monospace) within the TextView control by using the textStyle and typeface attributes.
- android:typeface="monospace" - You can use this attribute to set the font family to use for the text.