Create An Android TextView Programmatically in Kotlin




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.

Output

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.

<resources>
	<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>
</resources>

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"?>
<LinearLayout
	android:id="@+id/rootLayout"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

</LinearLayout>

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.support.v7.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val linearLayout = findViewById<LinearLayout>(R.id.rootLayout)

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

        // Add TextView to LinearLayout
        linearLayout?.addView(textView)
    }
}

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"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>
</manifest>

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