TextView in Android with Java

Introduction

In this article, we will learn about TextView in Android with Java Programming Language with different types of TextViews with example programs.

TextView in Android

TextView is the user interface that displays the text message on the screen to the user. It is based on the layout size, style, color, etc. TextView is used to set and display the text according to our specifications.

In Android, when we create a new project with a specific name, Android provides us a TextView in our activity_main.xml file, and the second file is the MainActivity.java file for the coding of Java programming language.

Properties of TextView in Android

  • android:id-ID is an attribute used to define a TextView uniquely.
    android:id="@+id/text_view" 
  • android: layout_height- Height is the attribute that is used to set the height of the TextView. It can be like wrap_content, which means TextView will be the same size as the text that is given by the user, and match_parent, which means TextView will consume the size of the whole screen. It is not related to the text of the user, and we can also define the size of the text according to the user in DPs.
    android:layout_height="wrap_content" 
  • android:layout_width- Width is the attribute that is used to set the width of the TextView. Like the height attribute, it can also be like wrap_content, which means TextView will be the same size as the text that is given by the user, match_parent, which means TextView will consume the size of the whole screen. It is not related to the text of the user, and we can also define the size of the text according to the user in DPs.
    android:layout_width="wrap_content" 
  • android:layout_centerInParent- centerInParent is the attribute that is used to set the text in the center of the screen. It is an optional attribute of the TextView. It is set as true and false by the user.
    android:layout_centerInParent="true"
  • android: text- This attribute handles the text that is given by the user in the form of a string.
    android:text="Hello World!" 
  • android: hint- This attribute is used when there is no text given by the user. This hint attribute will show how the text will be displayed on the screen.
    android:hint="Hello Android" 
  • android:inputType- This attribute is used to set the type of the TextView. The type of text can be phone number, eMail, Password, etc.
    android:inputType="text" 
  • android:textSize- This attribute is used to set the size of the TextView. The size of the TextView given in the size of sp.
    android:textSize="30sp" 
  • android:textStyle- This attribute is used to set the style of the TextView. The style of the TextView is given as bold, italic, etc.
    android:textStyle="bold" 
  • android:textColorHighlight- This attribute is used to set the color of the text selection highlight.
    android:textColorHighlight="@color/colorPrimaryDark" 

Note. Here, we will create a new project for the TextView example in Android. Let's start working with Android Studio.

Step 1. First of all, when we will open the Android Studio we will see the screen with many options. We will choose the first option," Start a new Android Studio project"

New project in Android

Explanation. In the above image, we choose "Start a new Android Studio project", and create a new project for our TextView example.

Step 2

Choose the activity for the project

Explanation. In the above image, as we can see, there are many examples of the activities we want to create for our project. We will choose an "Empty Activity" for our example project for the TextView in Android.

Step 3. After choosing the activity type we will create our project as shown in the below image.

textview_example

Explanation. We will give the name "TextView Example" for our project and will click on the "Finish" button.

Note. Android Studio will create a project named "TextView Example". Android Studio will create two files in the project MainActivity.java and activity_main.xml.

Step 4. As we know, Android Studio creates two files. Now, we will see the activity_main.xml file of the project. The code of this file is listed below.

<?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">  
  
    <TextView  
        android:id="@+id/text_view"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Hello World!" />  
  
</RelativeLayout> 

Explanation. In the above code example, as we see, Android Studio provides a TextView as an example. In this example, we have RelativeLayout as the root Layout. In the TextView, we have four attribute ids for the identification of the TextView. Height and width are set as the wrap_content, so the TextView will take the space according to the text. The last attribute is the text that takes the text as the string.

Step 5. Now, we will see the MainActivity.java file. The code of the file "MainActivity.java" is listed below.

import androidx.appcompat.app.AppCompatActivity;  
import android.os.Bundle;  
  
public class MainActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
    }  
} 

Explanation. This is the code of our project's MainActivity.java file. Now, we will run our TextView Example project.

The following code of our project generates the following output.

textview_example

TextView examples with different attributes

Now, we will update our XML file of the project with different attributes of the TextView. Let's see the different examples of TextView.

Example 1 

<?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">  
  
    <TextView  
        android:id="@+id/text_view"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Hello World!"  
        android:textColor="@color/colorAccent"  
        android:textSize="30sp"  
        android:textStyle="bold" />  
  
</RelativeLayout>

Explanation. In this code example, we use attributes like textColor, textSize, and textStyle to make the textView more attractive, and here is the output of the code.

textview_output2

Example 2

<?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">  
  
    <TextView  
        android:id="@+id/text_view"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:padding="30dp"  
        android:text="Hello World!"  
        android:textColor="@color/colorAccent"  
        android:textSize="30sp"  
        android:textStyle="bold" />  
  
</RelativeLayout> 

Explanation. In this code example, we use attributes android: padding="30dp" for making the textView more attractive, and here is the output of the code.

textview_example3

Example 3

<?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">  
  
    <TextView  
        android:id="@+id/text_view"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Hello World!"  
        android:layout_centerInParent="true"  
        android:textColor="@color/colorAccent"  
        android:textSize="30sp"  
        android:textStyle="bold" />  
  
</RelativeLayout>        

Explanation. In this code example, we use the android: layout_centerInParent=" " attribute and set it as true, and here is the output of the code.

textview_output4

TextView in MainActivity.java

We see in the examples of the XML files to create different types of TextView. Now, we will see how to create the TextView in the MainActivity.java file through the Java Programming Language.

Let's see the example of the TextView in the MainAtivity.java file of our project. The code example is listed below.

<?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">  
  
    <TextView  
        android:id="@+id/text_view"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:hint="Text will show here..."  
        android:layout_centerInParent="true"  
        android:textColor="@color/colorAccent"  
        android:textSize="30sp"  
        android:padding="20dp"  
        android:layout_margin="10dp"  
        android:textStyle="bold" />  
  
</RelativeLayout> 

Explanation. In this example, we set the TextView in the activity_main.xml file and use the attributes like padding, margin, and centerInParent as true, so the TextView will show in the center. We use attribute android: hint= "Text will show here", so if we will set the TextView in the MainActivity.java file, but it will not set the text in the Java file, the text of the hint will be shown in the TextView.

Let's see the MainActivity.java file code in our project.

import androidx.appcompat.app.AppCompatActivity;  
import android.os.Bundle;  
import android.widget.TextView;  
  
public class MainActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        TextView txtView = findViewById(R.id.text_view);  
        txtView.setText("Android TextView");  
  
    }  
} 

Explanation. In this code example of MainActivity.java file, first of all, we access the TextView from the xml file by the line "TextView txtView = findViewById(R.id.text_view);" and in the next line, we set the text by the line "txtView.setText("Android TextView");"

Now we will run our project and here is the output of our project.

textview_example5

Summary

In this article, we learned about TextView in Android with Java and its types with examples.


Similar Articles