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.

Output

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.

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

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"?>
<LinearLayout
	android:id="@+id/activity_main"
	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
		android:id="@+id/editTextContainer"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical">
	</LinearLayout>

	<Button
		android:id="@+id/btnShow"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/show_text">
	</Button>

</LinearLayout>

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

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

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

        // Add EditText to LinearLayout
        linearLayout?.addView(editText)

        val btnShow = findViewById<Button>(R.id.btnShow)
        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"
		  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>

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

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