Displaying Text to Users with TextView Android

One of the most basic user interface elements, or controls, in the Android SDK is the TextView control. You use it, quite simply, to draw text on the screen. You primarily use it to display fixed text strings or labels.

Frequently, the TextView control is a child control within other screen elements and controls. As with most of the user interface elements, it is derived from View and is within the android.widget package. Because it is a View, all the standard attributes such as width, height, padding, and visibility can be applied to the object. However, as a text-displaying control, you can apply many other TextView-specific attributes to control behavior and how the text is viewed in a variety of situations.

First, though, let’s see how to put some quick text up on the screen. <TextView> is the XML layout file tag used to display text on the screen. You can set the android:text property of the TextView to be either a raw text string in the layout file or a reference to a string resource.

Here are examples of both methods you can use to set the android:text attribute of a TextView. The first method sets the text attribute to a raw string; the second method uses a string resource called sample_text, which must be defined in the strings.xml resource file.

To display this TextView on the screen, all your Activity needs to do is call the setContentView() method with the layout resource identifier in which you defined the preceding XML shown.You can change the text displayed programmatically by calling the setText() method on the TextView object. Retrieving the text is done with the< getText() method.

Now let’s talk about some of the more common attributes of TextView objects.

Configuring Layout and Sizing

The TextView control has some special attributes that dictate how the text is drawn and flows. You can, for instance, set the TextView to be a single line high and a fixed width. If, however, you put a long string of text that can’t fit, the text truncates abruptly. Luckily, there are some attributes that can handle this problem.

The width of a TextView can be controlled in terms of the ems measurement rather than in pixels. An em is a term used in typography that is defined in terms of the point size of a particular font. (For example, the measure of an em in a 12-point font is 12 points.)

This measurement provides better control over how much text is viewed, regardless of the font size.Through the ems attribute, you can set the width of the Text View.Additionally, you can use the maxEms and minEms attributes to set the maximum width and minimum width, respectively, of the TextView in terms of ems.

The height of a TextView can be set in terms of lines of text rather than pixels.Again, this is useful for controlling how much text can be viewed regardless of the font size. The lines attribute sets the number of lines that the TextView can display.You can also use maxLines and minLines to control the maximum height and minimum height, respectively, that the Textview displays.

Here is an example that combines these two types of sizing attributes. This TextView is two lines of text high and 12 ems of text wide. The layout width and height are specified to the size of the TextView and are required attributes in the XML schema:

Instead of having the text only truncate at the end, as happens in the preceding example, we can enable the ellipsize attribute to replace the last couple characters with an ellipsis (...) so the user knows that not all text is displayed.

Creating Contextual Links in Text

If your text contains references to email addresses, web pages, phone numbers, or even street addresses, you might want to consider using the attribute autoLink. The autoLink attribute has four values that you can use in combination with each other. When enabled, these autoLink attribute values create standard web-style links to the application that can act on that data type. For instance, setting the attribute to web automatically finds and links any URLs to web pages.

Your text can contain the following values for the autoLink attribute:

  • none: Disables all linking.
  • web: Enables linking of URLs to web pages.
  • email: Enables linking of email addresses to the mail client with the recipient filled.
  • phone: Enables linking of phone numbers to the dialer application with the phone number filled out, ready to be dialed.
  • map: Enables linking of street addresses to the map application to show the location.
  • all: Enables all types of linking.

Turning on the autoLink feature relies on the detection of the various types within the Android SDK. In some cases, the linking might not be correct or might be misleading.

Three TextViews: Simple, AutoLink All (not clickable), and AutoLink All (clickable).

Three TextViews: Simple, AutoLink All (not clickable), and AutoLink All (clickable)

Here is an example that links email and web pages, which, in our opinion, are the most reliable and predictable:

There are two helper values for this attribute, as well. You can set it to none to make sure no type of data is linked. You can also set it to all to have all known types linked. Figure illustrates what happens when you click on these links. The default for a TextView is not to link any types. If you want the user to see the various data types highlighted but you don’t want the user to click on them, you can set the linksClickable attribute to false.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics