The Wayback Machine - https://web.archive.org/web/20240621050007/https://www.geeksforgeeks.org/textview-widget-in-android-using-java-with-examples/
Open In App

TextView widget in Android using Java with Examples

Last Updated : 02 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Widget refers to the elements of the UI (User Interface) that help the user interact 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:

java.lang.Object
â†ģ android.view.View
â†ģ android.widget.TextView

Syntax:

<SomeLayout>
.
.
<TextView>
android:SomeAttribute1 = "Value of attribute1"
android:SomeAttribute2 = "Value of attribute2"
.
.
android:SomeAttributeN = "Value of attributeN"
</TextView>
.
.
</SomeLayout>

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.

Example:

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="GeeksforGeeks" />

</LinearLayout>

XML Attributes of TextView in Android

AttributesDescription
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.

How to include a TextView in an Android App:

Step 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.

Step 2: Open the Activity file and include a TextView in this file. The code for the TextView will be:

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome To"
android:textSize="26sp"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Step 3: Now in the Java file, link this layout file with the below code:

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

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

Step 4: In the Java file, we will try to change the Text displayed on the TextView upon touching along with a Toast message.

Step 5: The complete code of the layout file and the Java file is given below.

Below is the implementation of the above approach:

MainActivity.java
package com.project.textview;

import androidx.appcompat.app.AppCompatActivity;
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;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Linking the activity file to this Java file
        setContentView(R.layout.activity_main);

        // Get the TextView with the id
        // mentioned in the layout file
        textView = (TextView)findViewById(R.id.textview);

        // Try to change the text of the Textview upon touch
        // and also display a Toast message
        textView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {

                // Changing text
                textView.setText("GeeksforGeeks");

                // Displaying Toast message
                Toast
                    .makeText(MainActivity.this,
                              "Welcome to GeeksforGeeks",
                              Toast.LENGTH_SHORT)
                    .show();
            }
        });
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome To"
        android:textSize="26sp"
        android:textStyle="bold"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

Output:





Previous Article
Next Article

Similar Reads

TextView in Android using Jetpack Compose
Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. Android TextView is a simple view in Android which is used to display text inside our App. In this article, we will take a look at the implementation of TextView in Android using Jetpack Compose. Attributes of TextView WidgetAttributes Descriptio
4 min read
How to add fading TextView animation in Android
TextView is the basic building block of user interface components. It is used to set the text and display it to the user. It is a very basic component and used a lot. A Fading TextView is a TextView that changes its content automatically every few seconds. If we want to design a beautiful interface than we can use Fading TextView. Approach: Add thi
1 min read
How to Display HTML in Textview along with Images in Android?
There are some situations when one needs to display rich formatted text in the app, like for a blog app or an app like Quora but Android's inbuilt function doesn't allow to display inline images by default, moreover they show the ugly blue line to the left of anything inside blockquote tag. Here is the simple solution to show HTML in TextView along
11 min read
Glow TextView in Android
By default Android doesn't provide a simple way to add the glow to the text view or any other view, so to make our app more attractive and beautiful we can add the glow effect to our TextView. In order to do so, we can use many different external libraries. A sample GIF is given below to get an idea about what we are going to do in this article. No
4 min read
Autosizing TextView in Android
If the user is giving the input and the input needs to be shown as TextView and if the user inputs the stuff which can go out of the screen, then in this case the font TextView should be decreased gradually. So, in this article, it has been discussed how the developer can reduce the size of TextView gradually as the user gives the data. One can hav
6 min read
How to Add a TextView with Rounded Corner in Android?
TextView is an essential object of an Android application. It comes in the list of some basic objects of android and used to print text on the screen. In order to create better apps, we should learn that how can we create a TextView which have a background with rounded corners. We can implement this skill in a practical manner for any android appli
2 min read
How to Make TextView and EditText Selectable in Android?
In this article, we are going to implement a very important feature related to TextView. While using any social media app or like using Facebook you may have seen there is a particular type of TextView which you cannot copy like a caption that people write on their posts. You can select the message but there few texts which cannot select or copy. S
3 min read
How to Increase or Decrease TextView Font Size in Android Programmatically?
In this App, we are going to learn how to increase or decrease TextView Font Size in android programmatically. Like we have seen that in many apps we sometimes want to enlarge the text. So here basically we are going to implement that. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going t
3 min read
How to Make Substring of a TextView Clickable in Android?
In this article, we are going to implement a very important feature related to TextView. Here we are making part of a string or a substring to act as a substring. This feature is important while writing a blog because it may be possible that for certain points we want to redirect the user to a link. So here we are going to learn how to implement th
2 min read
Expandable TextView in Android
ExpandableTextView is an Android library which allows us to easily create a TextView which can expand/collapse when user clicks on it .we can use this feature in many apps such as movie review app or storytelling app and in many other apps. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are go
4 min read