Open In App

Working With the TextView in Android

TextView in Android is one of the basic and important UI elements. This plays a very important role in the UI experience and depends on how the information is displayed to the user. This TextView widget in Android can be dynamized in various contexts. For example, if the important part of the information is to be highlighted then the substring that contains, it is to be italicized or it has to be made bold, one more scenario is where if the information in TextView contains a hyperlink that directs to a particular web URL then it has to be spanned with hyperlink and has to be underlined.

Operations Performed in TextView on Android

Have a look at the following list and image to get an idea of the overall discussion.

Working_With_the_TextView_in_Android

Step by Step Implementation

Step 1: Create an Empty Activity Project

Step 2: Working with the activity_main.xml file

To implement the UI of the activity invoke the following code inside the activity_main.xml file.

<?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=".MainActivity"
    tools:ignore="HardcodedText">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksforGeeks"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 
 Output UI:

Output_UI_TextView_1

1. Formatting the TextView

Android offers mainly 3 types of typefaces

  • normal
  • sans
  • serif
  • monospace

The above four types of faces are to be invoked under the "typeFace" attribute of the TextView in XML.

Invoke the following code and note the "typeFace" attribute of the TextView.

<?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=".MainActivity"
    tools:ignore="HardcodedText">

    <!--the below typeFace attribute has to
         invoked with values mentioned-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksforGeeks"
        android:textSize="32sp"

        android:typeface="normal"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

  Output:

Android_Font_Type

2. Size of the TextView

The attribute which is used to change the size of the Text View in android is "textSize".

Refer to the following code and its output for better understanding.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"

        android:textSize="48sp"

        android:text="H3 Heading" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"

        android:textSize="32sp"

        android:text="H6 Heading" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"

        android:textSize="16sp"

        android:text="Body 1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"

        android:textSize="14sp"

        android:text="Body 2" />


</LinearLayout>

 
 Output:  


Heading_TextView

3. Changing Text Style

In Android there are basically three text styles:

  • Bold
  • Italic
  • Normal

To implement the various text styles refer to the following code and its output.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:orientation="vertical">

        <!--the below textStyle attribute has to 
            invoked with values mentioned-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GeeksforGeeks"
            
            android:textStyle="italic"

            android:textSize="32sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="GeeksforGeeks"
                  
            android:textStyle="bold"

            android:textSize="32sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="GeeksforGeeks"
                  
            android:textStyle="normal"

            android:textSize="32sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="GeeksforGeeks"
                  
            android:textStyle="bold|italic"

            android:textSize="32sp" />

    </LinearLayout>

</LinearLayout>

Output:


Various_Context_view

4. Changing the Text Color

Refer to the following code and its output for better understanding. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <!--the value predefined by android-->
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:text="Warning Message"
        android:textColor="#B00020"
        android:textSize="32sp" />

    <!--the value predefined by android-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:text="Disabled Text"

        android:textColor="@android:color/darker_gray"

        android:textSize="32sp" />

    <!--the value is hex code-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:text="GeeksforGeeks"

        android:textColor="#000000"

        android:textSize="32sp" />

</LinearLayout>

Output: 

Text_Color

5. Text Shadow

android:shadowDx="integer_value" -> which decides the distance of text from its shadow with respect to x axis, if the integer_value is positive the shadow is on positive of the x axis and vice versa.

android:shadowDy="integer_value" -> which decides the distance of text from its shadow with respect to y axis, if the integer_value is positive the shadow is on negative of the y axis and vice versa.

android:shadowRadius="integer_value" -> which decides the amount of the shadow to be given for the text view.

Refer to the following code and its output for better understanding.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:shadowColor="@color/grey"
        android:shadowDx="4"
        android:shadowDy="4"
        android:shadowRadius="10"
        android:text="GeeksforGeeks"
        android:textColor="#000000"
        android:textSize="32sp"
        tools:targetApi="ice_cream_sandwich" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:padding="8dp"
        android:shadowColor="@color/grey"
        android:shadowDx="-15"
        android:shadowDy="4"
        android:shadowRadius="10"
        android:text="GeeksforGeeks"
        android:textColor="#000000"
        android:textSize="32sp"
        tools:targetApi="ice_cream_sandwich" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:shadowColor="@color/grey"
        android:shadowDx="4"
        android:shadowDy="-15"
        android:shadowRadius="10"
        android:text="GeeksforGeeks"
        android:textColor="#000000"
        android:textSize="32sp"
        tools:targetApi="ice_cream_sandwich" />

</LinearLayout>

Output:

Shadow_Text

6. Letter Spacing and All Caps

android:letterSpacing="floatingTypeValue" -> This attribute is used to give the space between each of the letters.

android:textAllCaps="trueOrfalse" -> This attribute decides, all the letters should be in uppercase or not.

Refer to the following code and its output for better understanding.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:letterSpacing="0.15"
        android:text="GeeksforGeeks"
        android:textColor="@android:color/black"
        android:textSize="32sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:text="GeeksforGeeks"
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="32sp" />

</LinearLayout>

 Output:

Letter_Spacing

7. Adding Icons for TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:drawableStart="@drawable/ic_lappy"
        android:padding="4dp"
        android:text="GeeksforGeeks"
        android:textColor="@android:color/black"
        android:textSize="32sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:drawableEnd="@drawable/ic_lappy"
        android:padding="4dp"
        android:text="GeeksforGeeks"
        android:textColor="@android:color/black"
        android:textSize="32sp" />

</LinearLayout>

 Output: 

Laptop_Icon_TextView


Click Here to Learn more to Create Your Own Android Kotlin App

Article Tags :