Skip to content
Related Articles
Open in App
Not now

Related Articles

TextView in Kotlin

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 29 Oct, 2021
Improve Article
Save Article

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 TextView in Kotlin:

  1. Add a TextView in activity_main.xml file inside LinearLayout.
  2. Add attributes like text, textColor, textSize, textStyle in the activity_main.xml file.
  3. Open MainActivity.kt file and set OnClickListener for the textView to show the Toast message.

Different attributes of TextView widgets –

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.

Modify the strings.xml file

We can add strings in the strings.xml file and use in the other files easily by calling them with their names.

    <string name="app_name">TextViewInKotlin</string>
    <string name="text_view">GeeksForGeeks</string>
    <string name="text_on_click">COMPUTER SCIENCE PORTAL</string>

activity_main.xml file

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

<?xml version="1.0" encoding="utf-8"?>
    <!--EditText with id editText-->

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

// finding the textView
 val textView = findViewById( as TextView

Setting the on click listener to the button

textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
                "COMPUTER SCIENCE PORTAL", Toast.LENGTH_LONG).show() }

MainActivity.kt file

Open app/src/main/java/yourPackageName/MainActivity.kt to get the reference of TextView.

package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            //accessing our textview from layout
            val textView = findViewById<TextView>( as TextView
            textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
                R.string.text_on_click, Toast.LENGTH_LONG).show() }

AndroidManifest.xml file

We are also going to see the code inside main/AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Run as emulator for output:

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!