Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Diving deep into TextView syntax
3.
Creating your first TextView
3.1.
Creating an Android Studio Project
3.2.
The main activity XML file
3.3.
The main activity Kotlin file
3.4.
The Android Manifest XML file
3.5.
The Resources directory
4.
Final Application
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Text View in Android

Author Vasu Bansal
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, you will learn about the TextView widget, which is commonly used in Android applications. Understanding the concept of widgets and user interface (UI) is necessary before proceeding further in the blog. The user interface refers to the frontend or the screen with which the user interacts directly. Widgets are the elements of this user interface that helps provide a smooth interface for the users. 

Diving deep into TextView syntax

TextView is a widget that helps the developer display some text messages on the application screen. There are a lot of properties associated with TextView, such as font size, font color, themes, gravity, etc. The following is the class syntax of  TextView.

public class TextView
extends View
implements ViewTreeObserver.OnPreDrawListener

The class hierarchy of TextView is as follows.

java.lang.Object
|-> android.view.View
  |-> android.widget.TextView

There are many other widgets available in android. To learn about ListView in Android development, refer to the blog ListView on the Coding Ninjas Website.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Creating your first TextView

Creating a TextView is a simple and straightforward process. Just follow the simple steps given in below to make your first TextView.

Creating an Android Studio Project

You need to first create an Android Studio project before actually creating your Android apps. To create a new project, open up Android Studio, press the New Project button, select Empty Activity as the activity type, and select the implementation language as Kotlin. Select a minimum SDK version as per your requirements.

Don't have Android Studio and environment already set up in your machine? No worries, we got you all covered. Refer to the blog Android Studio and Environment Setup on the Coding Ninjas Website to set up things for a smoother development process.

The main activity XML file

The activity_main.xml file is the most crucial layout of your application which is referenced when we are building the interface of our application. The code for the TextView widget is written in this file. Write the following code into this file.

Code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:layout_height="match_parent"
    android:layout_width="match_parent
">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Coding Ninjas"
        android:textSize="50sp"
        android:textStyle="bold"
        android:textColor="#FFA500"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

You can customize the attributes of this TextView by changing the values associated with the XML attributes of TextView. For example, in this case, I have assigned android:text as  Coding Ninjas and android:textStyle as bold. You can change such properties as per your requirements.

The main activity Kotlin file

This is the main file that gets your application running. This file contains the onCreate method, one of the first methods that get called after running our application. The code for MainActivity.kt file is given as below.

Code:

package com.example.textview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

The Android Manifest XML file

This file is used for registering our activities. We don’t need to change anything in this file and just use the boilerplate code provided by Android Studio for this. The code for the AndroidManifest.xml file is given below.

Code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textview">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TextView">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The Resources directory

The resources folder named res in our Android Studio project is an important folder that contains all the resources for our application. It contains all non-coding assets of our application. For example, consider the strings.xml file present under the directory res/values. This file contains the name of the application, which is rendered when we run the application. The code of this file is written below. In this blog, we have named the application as TextView.

Code:

<resources>
    <string name="app_name">TextView</string>
</resources>

Final Application

The final text view that is created after following this blog will look as follows.

FAQs

  1. Which attribute of the TextView is used to specify that the text type is password?
    Ans: The android:password attribute is used to specify that the text type is password. When this attribute is specified, the fields are shown as bulleted dots instead of the characters themselves.
     
  2. What is the use of the android:scrollHorizontally attribute in the XML file? 
    Ans: This property is used to satisfy whether the size of the text can be greater than the size of the view itself. If this attribute is set, then the text can be scrolled horizontally.
     
  3. Can I apply multiple text styles to the text written inside a TextView?
    Ans: Yes, you can assign multiple text styles to the text written inside a TextView with the help of the android:textStyle attribute. For example, you can apply a combination of bold and italic to a text by writing the value as bold|italic.

Key Takeaways

In this article, we have extensively discussed Text Views in Android along with a sample application containing the implementation of a sample Text View. You can also read the blog Navigation Drawer on the Coding Ninjas Website to create Navigation Drawers in your Android application.

We hope that this blog has helped you enhance your knowledge regarding Text Views and if you would like to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Android UI Control and Types
Next article
Edit Text Widget In Android
Live masterclass