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 which displays the text message on the screen to the user. It is based on the layout size, style, and 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.
    1. android:id="@+id/text_view" 
  • android: layout_height- Height is the attribute which is used to set the height of the TextView. It can be like wrap_content that means TextView will be the same size as the text that is given by the user, match_parent that 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.
    1. android:layout_height="wrap_content" 
  • android:layout_width- Width is the attribute which is used to set the width of the TextView. Like height attribute, it is also can 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.
    1. android:layout_width="wrap_content" 
  • android:layout_centerInParent- centerInParent is the attribute which 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.
    1. android:layout_centerInParent="true"
  • android: text- This attribute handles the text that is given by the user in the form of the string.
    1. android:text="Hello World!" 
  • android: hint- This attribute is used when there is no text is given by the user, in this hint attribute will show how the text will display on the screen.
    1. 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, and Password, etc.
    1. 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.
    1. android:textSize="30sp" 
  • android:textStyle- This attribute is used to set the style of the TextView. The style of the TextView given as bold, italic, etc.
    1. android:textStyle="bold" 
  • android:textColorHighlight- This attribute is used to set the color of the text selection highlight.
    1. 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.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/text_view"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Hello World!" />  
  14.   
  15. </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 is 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.
  1. import androidx.appcompat.app.AppCompatActivity;  
  2.   
  3. import android.os.Bundle;  
  4.   
  5. public class MainActivity extends AppCompatActivity {  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.     }  
Explanation
 
This is the code of the MainActivity.java file of our project. 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 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/text_view"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Hello World!"  
  14.         android:textColor="@color/colorAccent"  
  15.         android:textSize="30sp"  
  16.         android:textStyle="bold" />  
  17.   
  18. </RelativeLayout>
Explanation
 
In this code example, we use attributes like textColor, textSize, textStyle for making the textView more attractive, and here is the output of the code.
 
textview_output2
 
Example 2
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/text_view"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:padding="30dp"  
  14.         android:text="Hello World!"  
  15.         android:textColor="@color/colorAccent"  
  16.         android:textSize="30sp"  
  17.         android:textStyle="bold" />  
  18.   
  19. </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
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/text_view"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Hello World!"  
  14.         android:layout_centerInParent="true"  
  15.         android:textColor="@color/colorAccent"  
  16.         android:textSize="30sp"  
  17.         android:textStyle="bold" />  
  18.   
  19. </RelativeLayout>        
Explanation
 
In this code example, we use android: layout_centerInParent=" " attribute and set 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 the 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.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/text_view"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:hint="Text will show here..."  
  14.         android:layout_centerInParent="true"  
  15.         android:textColor="@color/colorAccent"  
  16.         android:textSize="30sp"  
  17.         android:padding="20dp"  
  18.         android:layout_margin="10dp"  
  19.         android:textStyle="bold" />  
  20.   
  21. </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 Java file the text of the hint will be shown in the TextView.
 
Let's see the MainActivity.java file code in our project.
  1. import androidx.appcompat.app.AppCompatActivity;  
  2.   
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5.   
  6. public class MainActivity extends AppCompatActivity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         TextView txtView = findViewById(R.id.text_view);  
  14.         txtView.setText("Android TextView");  
  15.   
  16.     }  
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 the TextView in Android with Java, and its types with examples.