Skip to content
Related Articles

Related Articles

Dynamic EditText in Kotlin
  • Last Updated : 18 Feb, 2021

EditText is used to get input from the user. EditText is commonly used in login or registration screens. we alerady
learn how to create an EditText using layout. In this article, we will learn about how to create android EditText programmatically in kotlin.

At first, we will create a new android application. Then, we will create an EditText dynamically.
If you already created the project then ignore step 1.

1. Create New Project

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

After creating project we will modify xml files then.

Modify activity_main.xml file

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




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
  
    <LinearLayout
        android:id="@+id/editTextLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
  
    <Button
        android:id="@+id/buttonShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show text">
    </Button>
  
</LinearLayout>


So in activity_main.xml file, we created a LinearLayout, having id editTextLinearLayout, we use this LinearLayout as a container for crearting EditText.



Create Android EditText Dynamically in Kotlin

Open app/src/main/java/net.geeksforgeeks.dynamicedittextkotlin/MainActivity.kt file
and add below code into it.




package com.geeksforgeeks.myfirstKotlinapp
  
import androidx.appcompat.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val editLinearLayout = findViewById<LinearLayout>(R.id.editTextLinearLayout)
        val buttonShow = findViewById<Button>(R.id.buttonShow)
  
        // Create EditText
        val editText = EditText(this)
        editText.setHint("Enter something")
        editText.layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        editText.setPadding(20, 20, 20, 20)
  
        // Add EditText to LinearLayout
        editLinearLayout?.addView(editText)
  
        buttonShow?.setOnClickListener { Toast.makeText(
            this@MainActivity, editText.text,
            Toast.LENGTH_LONG).show() }
    }
}


Here, we are created EditText Dynamically in kotlin. Then, Adding this EditText into LinearLayout, having id editTextLinearLayout. and a toast message is also shown when button is click.

As, AndroidManifest.xml file is very important file in android application, so below is the code of manifest file.

AndroidManifest.xml file

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




<?xml version="1.0" encoding="utf-8"?>
    package="net.geeksforgeeks.dynamicedittextkotlin">
  
    <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>


Run as Emulator:

Now, run your application. you will get output as shown below.

You can find the complete code here:
https://github.com/missyadavmanisha/DynamicEditTextKotlin

My Personal Notes arrow_drop_up
Recommended Articles
Page :