Open In App

TextView in Kotlin

Last Updated : 24 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Android TextView is simply a view that are used to display the text to the user and optionally allow us to modify or edit it. First of all, open Kotlin project in Android Studio. Following steps are used to create

Steps to Implement TextView

Steps by Step implementation for creating an application which contains TextView in Kotlin and pop-up toast when clicked on the text.

Step 1: Create a new Kotlin Android Application

To create a new project in Android Studio follow these steps:

  • Click on File, then New, and then New Project, and give a name whatever you like.
  • Choose “Empty Activity” for the project template.
  • Then, select Kotlin language Support and click the next button.
  • Select minimum SDK(According to the application need).

Step 2: Modification in activity_main.xml file

Open activity_main.xml file and create a TextView using id textView.

activity_main.xml:

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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_view_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40sp"
        android:text="GeeksForGeeks"
        android:textColor="#008000"
        android:textSize="40dp"
        android:textStyle="bold" />
</LinearLayout>

Design UI:

textview-in-kotlin


Step 3: Modification in MainActivity.kt file

Open MainActivity.kt file and get the reference of TextView defined in the layout file.

// finding the textView
val textView: TextView = findViewById(R.id.text_view_id)

Setting the on click listener to the button

textView.setOnClickListener{
Toast.makeText(this, “COMPUTER SCIENCE PORTAL”, Toast.LENGTH_SHORT).show()
}

MainActivity.kt:

Java
package org.geeksforgeeks.demo

import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

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

        // Accessing our TextView from the layout
        val textView: TextView = findViewById(R.id.text_view_id)

        // Set an onClickListener to show a Toast message
        textView.setOnClickListener {
            Toast.makeText(this, "COMPUTER SCIENCE PORTAL", Toast.LENGTH_SHORT).show()
        }
    }
}

Output:


Different attributes of TextView Widgets

AttributesDescription
android:textSets text of the Textview
android:idGives a unique ID to the Textview
android:cursorVisibleUse this attribute to make cursor visible or invisible. Default value is visible.
android:drawableBottomSets images or other graphic assets to below of the Textview.
android:drawableEndSets images or other graphic assets to end of Textview.
android:drawableLeftSets images or other graphic assets to left of Textview.
android:drawablePaddingSets padding to the drawable(images or other graphic assets) in the Textview.
android:autoLinkThis attribute is used to automatically detect url or emails and show it as clickable link.
android:autoTextAutomatically correct spelling errors in text of the Textview.
android:capitalizeIt automatically capitalize whatever the user types in the Textview.
android:drawableRightSets drawables to right of text in the Textview.
android:drawableStartSets drawables to start of text in the Textview.
android:drawableTopSets drawables to top of text in the Textview.
android:ellipsizeUse this attribute when you want text to be ellipsized if it is longer than the Textview width.
android:emsSets width of the Textview in ems.
android:gravityWe can align text of the Textview vertically or horizontally or both.
android:heightUse to set height of the Textview.
android:hintUse to show hint when there is no text.
android:inputTypeUse to set input type of the Textview. It can be Number, Password, Phone etc.
android:linesUse to set height of the Textview by number of lines.
android:maxHeightSets maximum height of the Textview.
android:minHeightSets minimum height of the Textview.
android:maxLengthSets maximum character length of the Textview.
android:maxLinesSets maximum lines Textview can have.
android:minLinesSets minimum lines Textview can have.
android:maxWidthSets maximum width Textview can have.
android:minWidthSets minimum lines Textview can have.
android:textAllCapsShow all texts of the Textview in capital letters.
android:textColorSets color of the text.
android:textSizeSets font size of the text.
android:textStyleSets style of the text. For example, bold, italic, bolditalic.
android:typefaceSets typeface or font of the text. For example, normal, sans, serif etc
android:widthSets width of the TextView.


Next Article

Similar Reads