Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SEOFriendlyImages has a deprecated constructor in /home/tutorig3/public_html/wp-content/plugins/seo-image/seo-friendly-images.class.php on line 2

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/tutorig3/public_html/wp-content/themes/mts_interactive/options/php-po/php-po.php on line 185
Create An Android EditText Programmatically in Kotlin - Tutorialwing

Create An Android EditText Programmatically in Kotlin

Hello Readers! In this post, we will learn about how to create android EditText programmatically in kotlin. We will go through different steps that explains how to create and use android EditText programmatically in kotlin.

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


Tutorialwing  - Create EditText dynamically in Kotlin , Create EditText programmatically in Kotlin

Tutorialwing – Create EditText dynamically in Kotlin

Video Output

At first, we will create a new android application. Then, we will create and use this widget dynamically into this application. Please ignore the steps if you have already created the project.

1. Creating New Project

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicEditText. 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.

You may also visit post to create new project in android. This tutorial is covered in java. However, the steps are similar in kotlin too.

Now, we will modify xml files, values folder files. then, create EditText widget dynamically. Please follow the steps below.

2. Modify Values Folder

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

	<string name="app_name">DynamicEditTextUsingKotlin</string>
	<string name="enter_something">Enter Something...</string>
	<string name="show_text">Show Text</string>

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

3. Modify Layout Folder

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

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




In xml file, we have created a LinearLayout, having id editTextContainer, that will used as container for EditText widget created programmatically in kotlin file.

4. Create Android EditText Programmatically / Dynamically in Kotlin

Open app/src/main/java/com.tutorialwing.dynamicedittext/MainActivity.kt file and add below code into it. You can also visit post to use EditText in xml file in kotlin.

package com.tutorialwing.dynamicedittext

import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val linearLayout = findViewById<LinearLayout>(

        // Create EditText
        val editText = EditText(this)
        editText.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        editText.setPadding(20, 20, 20, 20)

        // Add EditText to LinearLayout

        val btnShow = findViewById<Button>(
        btnShow?.setOnClickListener { Toast.makeText(this@MainActivity, editText.text, Toast.LENGTH_LONG).show() }

Here, we are creating EditText programmatically in kotlin. Then, Adding this EditText into LinearLayout, having id editTextContainer. We are also showing toast message on button click.

Since, AndroidManifest.xml file is very important file in android application, we are also showing code into this file.

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file would look like below –

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

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

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


Now, run the application. We will get output as shown above.

That’s end of tutorial on Creating Android EditText programmatically in kotlin.