Open In App
Related Articles

TextView widget in Android using Java with Examples

Improve Article
Save Article
Like Article

Widget refers to the elements of the UI (User Interface) that helps user interacts with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app.

TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc set for that particular TextView. It optionally allows us to modify or edit itself as well.

Class Syntax:

public class TextView
  extends View 
  implements ViewTreeObserver.OnPreDrawListener

Class Hierarchy:

  ↳ android.view.View
     ↳ android.widget.TextView


        android:SomeAttribute1 = "Value of attribute1"
        android:SomeAttribute2 = "Value of attribute2"
        android:SomeAttributeN = "Value of attributeN"

Here the layout can be any layout like Relative, Linear, etc (Refer this article to learn more about layouts). And the attributes can be many among the table given below in this article.



        android:text="GeeksforGeeks" />


How to include a TextView in an Android App:

  1. First of all, create a new Android app, or take an existing app to edit it. In both the case, there must be an XML layout activity file and a Java class file linked to this activity.
  2. Open the Activity file and include a TextView in this file. The code for the TextView will be:
  3. Now in the Java file, link this layout file with the below code:
    protected void onCreate(Bundle savedInstanceState)

    where activity_main is the name of the layout file to be attached.

  4. In the Java file, we will try to change the Text displayed on the TextView upon touching along with a Toast message.
  5. The complete code of the layout file and the Java file is given below.

Below is the implementation of the above approach:


<?xml version="1.0" encoding="utf-8"?>
        android:text="Welcome To"
        app:layout_constraintTop_toTopOf="parent" />

package com.project.textview;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    // Creating the instance of the TextView created
    private TextView textView;
    protected void onCreate(Bundle savedInstanceState)
        // Linking the activity file to this Java file
        // Get the TextView with the id
        // mentioned in the layout file
        textView = (TextView)findViewById(;
        // Try to change the text of the Textview upon touch
        // and also display a Toast message
        textView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
                // Changing text
                // Displaying Toast message
                              "Welcome to GeeksforGeeks",


XML Attributes of TextView in Android

android:textSets text of the Textview
android:idGives a unique ID to the Textview
android:cursorVisibleUse this attribute to make cursor visible or invisible. Default value is visible.
android:drawableBottomSets images or other graphic assets to below of the Textview.
android:drawableEndSets images or other graphic assets to end of Textview.
android:drawableLeftSets images or other graphic assets to left of Textview.
android:drawablePaddingSets padding to the drawable(images or other graphic assets) in the Textview.
android:autoLinkThis attribute is used to automatically detect url or emails and show it as clickable link.
android:autoTextAutomatically correct spelling errors in text of the Textview.
android:capitalizeIt automatically capitalize whatever the user types in the Textview.
android:drawableRightSets drawables to right of text in the Textview.
android:drawableStartSets drawables to start of text in the Textview.
android:drawableTopSets drawables to top of text in the Textview.
android:ellipsizeUse this attribute when you want text to be ellipsized if it is longer than the Textview width.
android:emsSets width of the Textview in ems.
android:gravityWe can align text of the Textview vertically or horizontally or both.
android:heightUse to set height of the Textview.
android:hintUse to show hint when there is no text.
android:inputTypeUse to set input type of the Textview. It can be Number, Password, Phone etc.
android:linesUse to set height of the Textview by number of lines.
android:maxHeightSets maximum height of the Textview.
android:minHeightSets minimum height of the Textview.
android:maxLengthSets maximum character length of the Textview.
android:maxLinesSets maximum lines Textview can have.
android:minLinesSets minimum lines Textview can have.
android:maxWidthSets maximum width Textview can have.
android:minWidthSets minimum lines Textview can have.
android:textAllCapsShow all texts of the Textview in capital letters.
android:textColorSets color of the text.
android:textSizeSets font size of the text.
android:textStyleSets style of the text. For example, bold, italic, bolditalic.
android:typefaceSets typeface or font of the text. For example, normal, sans, serif etc
android:widthSets width of the TextView.

Last Updated : 01 Oct, 2021
Like Article
Save Article
Similar Reads
Related Tutorials