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
-
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.
-
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.
-
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!