Skip to content
Related Articles

Related Articles

Improve Article

Android EditText in Kotlin

  • Last Updated : 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:

  1. Add a EditText in activity_main.xml file.
  2. Add a Button in activity_main.xml file.
  3. Open MainActivity.kt file and set OnClickListner for the button to get the user input from EditText and show the input as Toast message.

Different attributes of EditText widgets –

XML AttributesDescription
android:idUsed to uniquely identify the control
android:gravityUsed to specify how to align the text like left, right, center, top, etc.
android:hintUsed to display the hint text when text is empty
android:textUsed to set the text of the EditText
android:textSizeUsed to set size of the text.
android:textColorUsed to set color of the text.
android:textStyleUsed to set style of the text. For example, bold, italic, bolditalic etc.
android:textAllCapsUsed this attribute to show the text in capital letters.
android:widthIt makes the TextView be exactly this many pixels wide.
android:heightIt makes the TextView be exactly this many pixels tall.
android:maxWidthUsed to make the TextView be at most this many pixels wide.
android:minWidthUsed to make the TextView be at least this many pixels wide.
android:backgroundUsed to set background to this View.
android:backgroundTintUsed to set tint to the background of this view.
android:clickableUsed to set true when you want to make this View clickable. Otherwise, set false.
android:drawableBottomUsed to set drawable to bottom of the text in this view.
android:drawableEndUsed to set drawable to end of the text in this view.
android:drawableLeftUsed to set drawable to left of the text in this view.
android:drawablePaddingUsed to set padding to drawable of the view.
android:drawableRightUsed to set drawable to right of the text in this view.
android:drawableStartUsed to set drawable to start of the text in this view.
android:drawableTopUsed to set drawable to top of the text in this view.
android:elevationUsed to set elevation to this view.

activity_main.xml file

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

MainActivity.kt file

Finally the MainActivity.kt file is




package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import 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()
        }
    }
}

AndroidManifest.xml file




<?xml version="1.0" encoding="utf-8"?>
    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>

Run as emulator for the Output

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!



My Personal Notes arrow_drop_up
Recommended Articles
Page :