StackTips
 7 minutes

Android TextView Example

By Nilanchala @nilan, On Mar 27, 2024 Android 2.43K Views

In the course of this tutorial, we will take a look into Android TextView widget and various TextView properties.

TextView is one of the most fundamental Android user interface widget, which is used to display text on an Android screen. TextView is like a dummy label, doesn’t allow to edit text input.

Following are some of the most used TextView properties.

Attribute NameRelated MethodDescription
android:autoLinksetAutoLinkMask(int)Automatically convert the links such as web url or email.
android:autoTextsetKeyListener(KeyListener)Auto spelling correction for text.
android:ellipsizesetEllipsize(TextUtils.TruncateAt)If the width required by text is longer than the View itself, it ellipsized text.
android:emssetEms(int)Makes the TextView be exactly this many ems wide.
android:gravitysetGravity(int)Specifies how to align the text by the view’s x- and/or y-axis when the text is smaller than the view.
android:lineSpacingMultipliersetLineSpacing(float,float)
android:linessetLines(int)Makes the TextView be exactly this many lines tall.
android:maxLinessetMaxLines(int)Makes the TextView be at most this many lines tall.
android:minLinessetMinLines(int)Makes the TextView be at least this many lines tall.
android:minWidthsetMinWidth(int)Makes the TextView be at least this many pixels wide.
android:shadowColorsetShadowLayer(float, float, float,int)Place a blurred shadow of text underneath the text, drawn with the specified color.
android:shadowDxsetShadowLayer(float, float, float, int)Horizontal offset of the text shadow.
android:shadowDysetShadowLayer(float, float, float, int)Vertical offset of the text shadow.
android:shadowRadiussetShadowLayer(float, float, float, int)Blur radius of the text shadow.
android:textsetText(CharSequence, TextView.BufferType)Text to display.
android:textAllCapssetAllCaps(boolean)Present the text in ALL CAPS.
android:textAppearanceBase text color, typeface, size, and style.
android:textColorsetTextColor(int)Text color.
android:textSizesetTextSize(int, float)Size of the text.
android:typefacesetTypeface(Typeface)Typeface (normal, sans, serif, monospace) for the text.

For all TextView properties and attributes follow official TextView documentation.

Also refer the following tutorials to learn more on Android TextView

How to apply shadow effect on Android TextView
How to use external fonts in Android TextView
How to Display HTML in Android TextView

TextView Example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:orientation="vertical"
    android:padding="10dp">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center_horizontal"
        android:text="stacktips.com"
        android:textAllCaps="true"
        android:textColor="#86AD33"
        android:textSize="25dp"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:ellipsize="end"
        android:letterSpacing="1.5"
        android:maxLines="2"
        android:text="Free programming tutorials on Java, Android development, Xamarin, Java Design Pattern, Data Structure Algorithm and examples on related technologies."
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="email|web"
        android:lineSpacingMultiplier="1.5"
        android:text="Have questions about stacktips.com or just want to chat? Contact us at [email protected]" />

</LinearLayout>
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.