Create An Android TextView Programmatically in Kotlin

We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at

Hello Readers! In this tutorial, you will learn how to create android Textview programmatically in kotlin programming language. You will go through various steps that explains how to create Textview and add it in kotlin file in any android application.

You may also visit post to know more about TextView and it’s useful xml attributes to customise it.


Tutorialwing Android TextView programmatically in kotlin Create TextView programmatically in android Create TextView dynamically in android in kotlin create TextView programmatically in kotlin

Tutorialwing Create TextView Programmatically Tutorial Output

Video Output

First step is to create New Project. After that, we will create textview dynamically and use it in our project. Follow the steps below to create new project. Please ignore the steps if you have already created project.

1. Creating New Project in Kotlin

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicTextView. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully.

We have also a detailed explanation on Creating New Android Project. Although tutorial is covered in Java, Steps are similar in kotlin as well.

Since we have a new project, we will modify the xml and class file to use TextView programmatically in kotlin. Please follow the steps below.

2. Modify Values Folder

Open res/values/strings.xml file. Add below code into it.

	<string name="app_name">DynamicTextViewUsingKotlin</string>
	<string name="clicked_on_me">You clicked on me.</string>
	<string name="click_on_me">Click on me</string>

Other values folders have not been changed. So, we are not going to mention it here.

3. Modify Layout Folder

Open res/layout/activity_main.xml file. Add below code into it.

<?xml version="1.0" encoding="utf-8"?>


Note that LinearLayout has id rootLayout. In Kotlin file, we will create TextView Dynamically and add it into this LinearLayout having id rootLayout.

4. Create Android TextView programmatically in Kotlin

Open src/main/java/com.tutorialwing.dynamictextview/MainActivity.kt file. Then, add below code into it.

package com.tutorialwing.dynamictextview

import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val linearLayout = findViewById<LinearLayout>(

        // Create TextView programmatically.
        val textView = TextView(this)
        textView.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        textView.gravity = Gravity.CENTER
        textView.setOnClickListener { Toast.makeText(this@MainActivity, R.string.clicked_on_me, Toast.LENGTH_LONG).show() }

        // Add TextView to LinearLayout

Note that we are creating TextView dynamically. Then, We have added it’s layoutParams, gravity and other attributes dynamically. Finally, added this TextView in LinearLayout having id rootLayout.

You may also visit post to create and use TextView widget programmatically in java.

AndroidManifest.xml file

Since AndroidManifest file is very important for project. We are also going to mention it here. Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.dynamictextview"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>

Finally, when you run the application, you will get output as shown above.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners