Android EditText in Kotlin - GeeksforGeeks

18 Feb, 2021
EditText is used to get input from the user. EditText is commonly used in forms and login or registration screens. Following steps are used to create EditText in Kotlin:
Add a EditText in activity_main.xml file.Add a Button in activity_main.xml file.Open MainActivity.kt file and set OnClickListner for the button to get the user input from EditText and show the input as Toast message.
Add a EditText in activity_main.xml file.
Add a Button in activity_main.xml file.
Open MainActivity.kt file and set OnClickListner for the button to get the user input from EditText and show the input as Toast message.
Step 1: Open activity_main.xml file and create an EditText using id editText.
<?xml version="1.0" encoding="utf-8"?><LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:context=".MainActivity">       <!--EditText with id editText-->      <EditText            android:id="@+id/editText"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="16dp"            android:hint="Input"            android:inputType="text"/>  </LinearLayout>
Step 2: In activity_main.xml file add code to show a button. Final activity_main.xml file is
<?xml version="1.0" encoding="utf-8"?><LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:context=".MainActivity">       <!--EditText with id editText-->      <EditText            android:id="@+id/editText"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="16dp"            android:hint="Input"            android:inputType="text"/>      <!--Button with id showInput-->      <Button            android:id="@+id/showInput"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:text="show"            android:backgroundTint="@color/colorPrimary"            android:textColor="@android:color/white"            />  </LinearLayout>
Step 3: Open MainActivity.kt file and get the reference of Button and EditText defined in the layout file.
        // finding the button
        val showButton = findViewById<Button>(R.id.showInput)
        
        // finding the edit text
        val editText = findViewById<EditText>(R.id.editText)

Setting the on click listener to the button
showButton.setOnClickListener {
            
        }

Getting the text entered by user
val text = editText.text

Finally the MainActivity.kt file is
package com.geeksforgeeks.myfirstkotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Buttonimport android.widget.EditTextimport android.widget.Toast  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // finding the button        val showButton = findViewById<Button>(R.id.showInput)          // finding the edit text        val editText = findViewById<EditText>(R.id.editText)          // Setting On Click Listener        showButton.setOnClickListener {              // Getting the user input            val text = editText.text              // Showing the user input            Toast.makeText(this, text, Toast.LENGTH_SHORT).show()        }    }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.geeksforgeeks.myfirstkotlinapp">      <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>
Android-View
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Broadcast Receiver in Android With Example
Services in Android with Example
How to Create and Add Data to SQLite Database in Android?
Content Providers in Android with Example
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Services in Android with Example
Content Providers in Android with Example
Android RecyclerView in Kotlin