Working With the TextView in Android - GeeksforGeeks (2024)

Last Updated : 21 Jun, 2024

Summarize

Comments

Improve

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.

Table of Content

  • Formatting the TextView
  • Size of the TextView
  • Changing Text Style
  • Changing the Text Color
  • Text Shadow
  • Letter Spacing and All Caps
  • Adding Icons for TextView
  • HTML Formatting of the TextView

Working With the TextView in Android - GeeksforGeeks (1)

Step by Step Implementation

Step 1: Create an Empty Activity Project

  • Create an empty activity Android Studio Project. Refer to Android | How to Create/Start a New Project in Android Studio? to know how To Create an empty activity Android Studio project.

Step 2: Working with the activity_main.xml file

  • The main layout and one, which includes only a TextView and as varied as we go on discuss the various contexts.

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

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=".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:

Working With the TextView in Android - GeeksforGeeks (2)

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
<?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:

Working With the TextView in Android - GeeksforGeeks (3)

2. Size of the TextView

  • This feature of the Text view upholds what type of content has to be shown to the user. For example, if there is a Heading, there are 6 types of heading that can be implemented have a look at the following image which contains the guidelines for the size of the text view and style of the text view which is recommended by Google’s Material Design.

Working With the TextView in Android - GeeksforGeeks (4)

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
<?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:

Working With the TextView in Android - GeeksforGeeks (5)

3. Changing Text Style

In Android there are basically three text styles:

  • Bold
  • Italic
  • Normal
  • The text style of the text in android can be implemented using the attribute “textStyle”.
  • Multiple text styles can also be implemented using the pipeline operator. Example “android:textStyle=”bold|italic”.

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

XML
<?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:

Working With the TextView in Android - GeeksforGeeks (6)

4. Changing the Text Color

  • The color of the text should also change according to the change in the context of the information displayed to the user.
  • For example, if there is warning text it must be in the red color and for disabled text, the opacity or the text color should be grayish. To change the color of the text, the attribute “textColor” is used.
  • Android also offers the predefined text colors, which can be implemented using “@android:color/yourColor” as value for the “textColor”. Here the value may be hex code or the predefined colors offered by the android.

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

XML
<?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:

Working With the TextView in Android - GeeksforGeeks (7)

5. Text Shadow

  • Shadow for the text can also be given in Android. The attributes required for the shadowed text view are:

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
<?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:

Working With the TextView in Android - GeeksforGeeks (8)

6. Letter Spacing and All Caps

  • Letter spacing and capital letters are some of the important properties of the text View in android.
  • For the text of buttons and tab layouts, the text should be in uppercase letters recommended by Google Material Design.
  • The letter spacing also should be maintained according to the scenario.

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
<?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:

Working With the TextView in Android - GeeksforGeeks (9)

7. Adding Icons for TextView

  • Android also allows adding drawable with the text views.
  • There are three positions to add the icons for the TextView. They are a start, end, top, and bottom.
  • Refer to the following code and its output, to know how to add the drawable icons to the Text View.
XML
<?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:

Working With the TextView in Android - GeeksforGeeks (10)

8. HTML Formatting of the TextView

  • In this article we are going to cover how to format and add different styles to a TextView in android by the help of HTML.

activity_main.xml :

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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GFG" android:id="@+id/tv_gfg" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

MainActivity Program to add functionality to the Layout:

MainAcivity.kt
package com.example.gfgimport android.os.Bundleimport android.text.Htmlimport android.widget.TextViewimport androidx.activity.enableEdgeToEdgeimport androidx.appcompat.app.AppCompatActivityimport androidx.core.view.ViewCompatimport androidx.core.view.WindowInsetsCompatclass MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_main) val textView: TextView = findViewById(R.id.tv_gfg) val htmlText = """ <h2>Welcome to GFG TextView Tutorial</h2> <p>This is an GFG Tutorial about TextView <b>bold</b>, <i>italic</i>, and <u>underlined</u> text in a TextView using HTML formatting.</p> <p>You can also include <a href="https://www.geeksforgeeks.org/">links</a> and other HTML elements.</p> """ textView.text = Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT) }}

Output:

Working With the TextView in Android - GeeksforGeeks (11)

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



adityamshidlyali

Working With the TextView in Android - GeeksforGeeks (13)

Improve

Previous Article

TextView in Kotlin

Next Article

Autosizing TextView in Android

Please Login to comment...

Working With the TextView in Android - GeeksforGeeks (2024)
Top Articles
Trading Fees: What Do Brokers Charge to Trade? - SmartAsset
Which Account Should You Tap First in Retirement?
Craigslist Cars Augusta Ga
Euro (EUR), aktuální kurzy měn
Byrn Funeral Home Mayfield Kentucky Obituaries
Activities and Experiments to Explore Photosynthesis in the Classroom - Project Learning Tree
The Realcaca Girl Leaked
Richard Sambade Obituary
C-Date im Test 2023 – Kosten, Erfahrungen & Funktionsweise
Nj Scratch Off Remaining Prizes
Valentina Gonzalez Leak
The Witcher 3 Wild Hunt: Map of important locations M19
Top tips for getting around Buenos Aires
This Modern World Daily Kos
Lima Funeral Home Bristol Ri Obituaries
Craigslist Farm And Garden Cincinnati Ohio
The Cure Average Setlist
Sam's Club La Habra Gas Prices
Craigslist Red Wing Mn
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
How To Cancel Goodnotes Subscription
Accident On The 210 Freeway Today
Grimes County Busted Newspaper
Bjerrum difference plots - Big Chemical Encyclopedia
Craigslist St. Cloud Minnesota
Craigslist Apartments Baltimore
The Listings Project New York
Essence Healthcare Otc 2023 Catalog
Tomb Of The Mask Unblocked Games World
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
101 Lewman Way Jeffersonville In
Yu-Gi-Oh Card Database
24 Hour Drive Thru Car Wash Near Me
Evil Dead Rise - Everything You Need To Know
Sf Bay Area Craigslist Com
Salons Open Near Me Today
Pokemmo Level Caps
Kagtwt
Reli Stocktwits
No Hard Feelings Showtimes Near Tilton Square Theatre
Santa Cruz California Craigslist
Jewish Federation Of Greater Rochester
Collier Urgent Care Park Shore
Fapello.clm
Pgecom
How the Color Pink Influences Mood and Emotions: A Psychological Perspective
552 Bus Schedule To Atlantic City
Colin Donnell Lpsg
Grand Park Baseball Tournaments
Morbid Ash And Annie Drew
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6192

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.