Open In App

Dynamic TextView in Kotlin

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

Android TextView is an user interface that is used to display some text to the user. In this article we will be discussing how to programmatically create a TextView in Kotlin .

Step by Step Implementation

Step 1: Create a new project

Let’s start by first creating a project in Android Studio. To do so, follow these instructions:

  • Click on File, then New and then New Project and give name whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need.
  • Select Empty activity and then click finish.

Step 2: Modify activity_main.xml file

Second step is to design our layout page. Here, we will use the RelativeLayout to get the TextView from the Kotlin file.

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

</LinearLayout>


Step 3: Create TextView in MainActivity.kt file

Navigate to app > src > main > java/kotlin > {package-name} > MainActivity.kt. In this file, we declare a variable TextView to create the TextView widget like this:

val textView = TextView(this)

textView.layoutParams= LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)

then add the widget in layout using this:

layout.addView(textView)

MainActivity File:

Kotlin
package org.geeksforgeeks.demo

import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.ViewGroup
import android.widget.LinearLayout
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)

        val layout: LinearLayout = findViewById(R.id.main)

        // Create TextView programmatically.
        val textView = TextView(this)

        // setting height and width
        textView.layoutParams= LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        // setting text
        textView.text = "GeeksforGeeks"
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 32f)
        textView.setTextColor(getColor(R.color.colorPrimary))
        // onClick the text a message will be displayed "HELLO GEEK"
        textView.setOnClickListener() {
            Toast.makeText(this@MainActivity, "HELLO GEEK",
                Toast.LENGTH_LONG).show()
        }

        // Add TextView to LinearLayout
        layout.addView(textView)
    }
}

Output:


Next Article

Similar Reads