TextView
TextView Attributes
You can use XML attributes for a TextView
to control:
Where the
TextView
is positioned in a layout (like any other view)How the
TextView
itself appears, such as with a background colorWhat the text looks like within the
TextView
, such as the initial text and its style, size, and color
For example, to set the width, height, and initial text value of the view:
You can extract the text string into a string resource (perhaps called hello_world
) that's easier to maintain for multiple-language versions of the app, or if you need to change the string in the future. After extracting the string, use the string resource name with @string/
to specify the text:
In addition to android:layout_width
and android:layout_height
(which are required for a TextView
), the most often used attributes with TextView
are the following:
Use one of the following with android:autoLink
:
none
: Match no patterns (default).web
: Match web URLs.email
: Match email addresses.phone
: Match phone numbers.map
: Match map addresses.all
: Match all patterns (equivalent to web|email|phone|map).
For example, to set the attribute to match web URLs, use android:autoLink="web"
.
Referring to a TextView in code
To refer to a TextView
in your Java code, use its resource id
. For example, to update a TextView
with new text, you would:
In which view_id is the resource identifier for the view (such as
show_count
) :
Last updated
Was this helpful?