Dynamic EditText in Kotlin - GeeksforGeeks

11 Nov, 2021
EditText is used to get input from the user. EditText is commonly used in login or registration screens. we alreadylearn 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
After creating project we will modify xml files then.
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"    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/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.
Open app/src/main/java/net.geeksforgeeks.dynamicedittextkotlin/MainActivity.kt fileand add below code into it.
package com.geeksforgeeks.myfirstKotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.ViewGroupimport android.widget.Buttonimport android.widget.EditTextimport android.widget.LinearLayoutimport 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.
Code inside src/main/AndroidManifest.xml file would look like below.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    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>
Now, run your application. you will get output as shown below.
You can find the complete code here:https://github.com/missyadavmanisha/DynamicEditTextKotlin
simmytarika5
Android-View
Kotlin Android
Picked
Android
Kotlin
Technical Scripter
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Resource Raw Folder in Android Studio
Services in Android with Example
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Android UI Layouts
Kotlin Array
Services in Android with Example
Android RecyclerView in Kotlin