Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Dynamic EditText in Kotlin

  • Last Updated : 11 Nov, 2021

EditText is used to get input from the user. EditText is commonly used in login or registration screens. we already
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.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

    1. Create New Project

    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"?>
              android:text="Show text">

      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 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 editLinearLayout = findViewById<LinearLayout>(
              val buttonShow = findViewById<Button>(
              // Create EditText
              val editText = EditText(this)
              editText.setHint("Enter something")
              editText.layoutParams = LinearLayout.LayoutParams(
              editText.setPadding(20, 20, 20, 20)
              // Add EditText to LinearLayout
              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"?>
                <activity android:name=".MainActivity">
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />

        Run as Emulator:

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

        You can find the complete code here:

        My Personal Notes arrow_drop_up
        Recommended Articles
        Page :