TextView in Android using Kotlin

The Android TextView widget is used to display the text on the screen such as headings or paragraphs. Steps to add TextView in Android activity.

  1. Add a TextView in the XML layout file of yours activity.
  2. Add required attributes such as text, textColor, textSize, textStyle to textView widget.
  3. Open Kotlin file to modify attributes of textView and to set Listeners for the textView.

Adding TextView in the layout file:

As you can see here we have added a TextView into our layout file. We have also added some attributes to our TextView such as text, textSize, textStyle, etc (You can add attributes to the widget according to requirements of style).

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />


List of available attributes for TextView widget.

Attributes Description
android:text Sets the text of the Textview
android:id Gives a unique ID to the Textview
android:cursorVisible Use this attribute to make the cursor visible or invisible. The default value is visible.
android:drawableBottom Sets images or other graphic assets below of the Textview.
android:drawableEnd Sets images or other graphic assets to end of Textview.
android:drawableLeft Sets images or other graphic assets to the left of Textview.
android:drawablePadding Sets padding to the drawable(images or other graphic assets) in the Textview.
android:autoLink This attribute is used to automatically detect URL or emails and show it as clickable links.
android:autoText Automatically correct spelling errors in the text of the Textview.
android:capitalize It automatically capitalizes whatever the user types in the Textview.
android:drawableRight Sets drawable to the right of the text in the Textview.
android:drawableStart Sets drawable to start of text in the Textview.
android:drawableTop Sets drawable to top of the text in the Textview.
android:ellipsize Use this attribute when you want the text to be ellipsize if it is longer than the Textview width.
android:ems Sets width of the Textview in ems.
android:gravity We can align the text of the Textview vertically or horizontally or both.
android:height Use to set the height of the Textview.
android:hint Use to show hints when there is no text.
android:inputType Use to set input type of the Textview. It can be Number, Password, Phone, etc.
android:lines Use to set the height of the Textview by the number of lines.
android:maxHeight Sets the maximum height of the Textview.
android:minHeight Sets the minimum height of the Textview.
android:maxLength Sets maximum character length of the Textview.
android:maxLines Sets maximum lines Textview can have.
android:minLines Sets minimum lines Textview can have.
android:maxWidth Sets maximum width Textview can have.
android:minWidth Sets minimum lines Textview can have.
android:textAllCaps Show all texts of the Textview in capital letters.
android:textColor Sets color of the text.
android:textSize Sets the font size of the text.
android:textStyle Sets style of the text. For example, bold, italic, bolditalic.
android:typeface Sets typeface or font of the text. For example, normal, sans, serif, etc
android:width Sets width of the TextView.


Set click listener to TextView using Kotlin:

In the following code firstly we linked a variable with our TextView widget with the help of the findViewById method, Then we set up an onClick listener to our TextView widget to show toast in case someone will click on the widget.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

        val textView = findViewById<TextView>(R.id.textView) as TextView

        textView.setOnClickListener(View.OnClickListener {
            Toast.makeText(this@MainActivity, "You have Clicked on TextView", Toast.LENGTH_SHORT).show()


Here is the final output:

TextView in Android using Kotlin

Spread the love
Scroll to Top