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

Assets folder in Android

You might have noticed that unlike the Eclipse ADT (App Development Tools), the Android Studio does not have an Assets folder, which is mainly used to store the web resources...

3 minutes read.

ImageView Widget in Android

What you should already KNOW? You should be familiar with: 1) Creating, building, and running apps in Android Studio 2) Basics of android widgets Explanation: Image View widget is used to insert the image...

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

Android ScrollView

A ScrollView is a view group that is used to create vertically scrollable views in an android application. Only a single direct child can be contained by ScrollView. For placing...

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

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.

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.

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.

Top Programming Languages for Android Development

For mobile apps, there are so many programming languages. You would need to spend days simply researching if you had to pick one from a list of 30 coding languages...

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

How to show image on an android application

ImageView Widget in Android What you should already KNOW? You should be familiar with: 1) Creating, building, and running apps in Android Studio 2) Basics of android widgets   Explanation: Image View widget is used...

2 minutes read.

Android Styles and Themes

Android styles allow you to design the appearance (for example, colors, size, and fonts) of Android UI components from XML resource files. With this method, you only need to set...

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

Android ListView

A kind of AdapterView that displays a vertical list of scrollable views, each of which is laid out below each other is called ListView. Using adapters, items are inserted into...

4 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 Toast

Android is one of the most successful operating systems available in today's world. It is an open-source phone platform, compatible with almost all the devices. There are numerous features and...

3 minutes read.

Android Emulator

A tool for creating Android virtual devices (including software and virtual hardware) on your personal desktop is known as Emulator. We can use these virtual devices as the target device...

4 minutes read.

Different Ways to Create aar File in Android Studio

A file called an Android archive file (*.aar) can be created using Android Studio and comprises classes and methods that use related files and classes for Android. The Android library...

4 minutes read.

How to Generate QRCode in Android?

Many apps use QR codes to show data in a way that is machine-readable. Data is represented using these codes in a secure manner such that only computers, not humans,...

4 minutes read.

Event Handling in Android

Events are a convenient way to gather data about user interactions with interactive elements of your application. Like a button click or a screen touch. A queue of events on...

3 minutes read.