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
|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.
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.
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.
Run as Emulator:
Now, run your application. you will get output as shown below.
You can find the complete code here: