Android TextView

Widgets are user interface (UI) elements that help users to interact with Android applications. It is one of many equipments that can be used to improvise the user interface of your application. TextView refers to a widget that displays text on the screen according to the layout, size, color, etc. set in a specific TextView. You can modify or edit the options themselves.

Structure:

public class TextView
  extends View 
  implements ViewTreeObserver.OnPreDrawListener

Class Hierarchy:

java.lang.Object
android.view.View
android.widget.TextView

Syntax:

<Any_Layout>
    .
    .
<TextView>
        android:Attribute1 = " attribute1 value"
        android:Attribute2 = "attribute2 value"
.
        android:AttributeN = "attributeN value"
</TextView>
    .
    .
</Any_Layout>

The layout here can be any layout such as Constraint, Relative, Linear, etc. (For more information on layout, see the article on Basic Layouts). Additionally, this documentcontainsinformation about a number of attributes in the table.

Example:

<androidx.constraintlayout.widget.ConstraintLayout 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=".ExampleActivity3">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Text View Example"
android:textColor="#151515"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Android TextView

Steps for including a TextView in an Android Application:

1. The first will be to create a new Android project or open an existing project to edit it in the Android Studio. Under any scenario, there must be an activity java class file and an XML layout activity file linked to the class.

2. Open the activity layout file and inserta textview in the layout.

3. Now in the SecondActivity.java file, connect theXML layout file as follows:

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


setContentView(R.layout.activity_second);
//activity_second is the name of the XML layout file to be displayed.
}

4. Let’s change the Text displayed in the TextView and show a toast when clicked.

5. The XML layout file and the Java file codesare as follows:

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".SecondActivity">
<TextView
android:id ="@+id/id1"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="Welcome To"
android:textSize ="26sp"
android:textStyle ="bold"
android:textColor ="@color/teal_200"
app:layout_constraintBottom_toBottomOf ="parent"
app:layout_constraintLeft_toLeftOf ="parent"
app:layout_constraintRight_toRightOf ="parent"
app:layout_constraintTop_toTopOf ="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

SecondActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class SecondActivity extends AppCompatActivity {


// Creating the object
publicTextView txt;


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


setContentView(R.layout.activity_second);
txt= findViewById(R.id.id1);


// Changing the text of the Textview upon click
        // and also show a Toast message
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
            {
// Setting new text
txt.setText("Paradise");
// Showing Toast
Toast
.makeText(SecondActivity.this,
"Welcome to Paradise",
Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }
}
Android TextView
On Click
Android TextView

Attributes of TextView

AttributesDescription
android:textSets the text of the Textview
android:idAssigns a distinct ID to the Textview
android:cursorVisibleUse this property to show or hide the cursor. Default values are displayed.
android:drawableBottomPlace an image or other graphic asset below the Textview.
android:drawableEndPlace an image or other graphic asset to the Textview’s end.
android:drawableLeftPlace an image or other graphic asset to the Textview’s left.
android:drawablePaddingUtilized to place padding to the in the Textview.
android:autoTextSpelling errors are corrected automatically in the text of the Textview.
android:capitalizeIt automatically capitalizesthe text that the user enters in the Textview.
android:drawableRightPlaces drawable to the text’s right in a Textview.
android:drawableStartPlaces drawable to text’s start in a Textview.
android:drawableTopPlaces drawable to text’s top in a Textview.
android:ellipsizeUtilize this property when you want the text to be ellipsiswhen the text’s length is longer than the width of Textview.
android:emsChanging the Textview’s width in ems.
android:gravityText of the Textview can be aligned either vertically or horizontally or both.
android:heightUtilize to change theTextview’s height.
android:linesUtilize to changethe height of the Textview by the number of lines.
android:maxHeightChange the Textview’s maximum height.
android:minHeightChange the Textview’s minimum height.
android:maxLengthChange the Textview’s maximum character length.
android:maxLinesDefines maximum lines that aTextview can contain.
android:minLinesDefines minimum lines that a Textview can contain.
android:maxWidthChange the maximum width that the Textview can have.
android:minWidthSets minimum width that the Textview can have.
android:textAllCapsCapitalize all text in Textview.
android:textColorChangethe text’s color.
android:textSizeChangethe text’sfont size.
android:textStyleChangethe text’s style. For example, bold, italic.
android:typefaceChangethe text typeface or font. For example,Arial, normal, sans, serif, etc
android:widthChanges width of the TextView.

Related Topics

Android 9.0 Pie vs. Android 10, which one is better

Android smartphone users are always searching for new system upgrades and keep an eye on system updates whenever the next version is published. Android 9 Pie and Android Q 10 are...

5 minutes read.

Android Date picker example

How to pick date from the user in Android? Write an android example to pick date from the user and display it. Android provides control for the user to pick a date...

3 minutes read.

Android Activity

What is an Activity? Android Activity is a screen in the user interface of an Android application. In this way, Android activity is very similar to the desktop application window. An...

4 minutes read.

Android GridView

Theview group that displays items in a two-dimensional scrolling grid is called GridView. The data isadded into this grid layout from anArray List or database. The adapter class isutilized to...

4 minutes read.

How to crop Image from Camera and Gallery in Android?

In this tutorial, we'll learn how to crop photos in the Android Gallery and Camera. There is no crop feature in this project, contrary to what was covered in the...

5 minutes read.

Broadcast Receiver in Android with Example

When a device wakes up, receives a message, receives an incoming call, switches to aeroplane mode, or initiates any other system-wide action, it is said to have been broadcast in...

5 minutes read.

Content Providers of Android with Example

An essential part of Android that performs the function of a relational database by storing application data is called a content provider. The function of the content provider in the...

13 minutes read.

How to insert an email in android application in android version 8

Email Address Insertion It is used to insert email adrress from the user in the android application.The only point of difference between an EditText and an Emailtext is that EmailText consisits...

1 minute read.

Creating a Calendar View App in Android

This article demonstrates how to make an Android application that uses CalendarView to display the Calendar. It also provides the option to pick the current date and view the date....

3 minutes read.

Android Tutorial

This Android tutorial will provide you with all the concepts that will be beneficial for both beginners and advanced-level students or candidates. You can learn this tutorial to begin your...

6 minutes read.

What is Android Studio

Android Studio powered by the IntelliJ platform is an integrated development environment (IDE) and is officially recognized for developing androidapplications. Its foundation lies on JetBrains' IntelliJ IDEA and is specialized...

3 minutes read.

Android Project Folder Structure

JetBrains community developed Android Studio, the official IDE (Integrated Development Environment) for Android app development and is freely distributed by Google. After the complete Android Architecture setup, we can construct an...

4 minutes read.

How to Update Gradle in Android Studio?

The Android Gradle plugin adds a number of features that are unique to develop Android apps to the Gradle build system, which is the foundation of the Android Studio development...

2 minutes read.

Android RecyclerView

RecyclerView is a ViewGroup brought to AndroidStudio as successor to ListView and GridView. These are all improvements and are included in the latest v7 support package. It was written to...

8 minutes read.

Dashboard UI Design in Android

One of the essential components that draw the user’s attentions into the application's operation is the dashboard design. It offers details on the application's general functionality in one location. Imagine...

11 minutes read.

Android Studio's Layout Editor

The Layout Editor allows you to efficiently create layouts by dragging UI elements from the Palette into the visual design editor instead of manually creating the layout XML file by...

8 minutes read.

AdapterViewFlipper in Android with Example

The AdapterViewFlipper class, a subclass of the ViewAnimator class, is what we use to switch from two or multiple views when only one is present at a time. The AdapterViewFlipper...

10 minutes read.

How to reduce the size of APK in Android

What exactly is an APK file? The full form of APK is Android Package Kit. It is the Android operating system's application file format. We use Android Studio to generate APK...

7 minutes read.

Android TextView

Widgets are user interface (UI) elements that help users to interact with Android applications. It is one of many equipments that can be used to improvise the user interface of...

4 minutes read.

Android Broadcast Receivers

Android applications can send or receive broadcast messages from the Android operating system and other Androidapplications, like the publish-subscribe design pattern. When an event of interest happens,these broadcasts are sent. For...

4 minutes read.