Open In App

Android EditText in Kotlin

Last Updated : 31 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

EditText is a widget in Android, that is used to get input from the user. EditText is commonly used in forms and login or registration screens. EditText extends the TextView class and provides more functionalities including handing text inputs such as cursor control, keyboard display and text validation.

Example:

<EditText
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:hint=”Enter a message”
android:inputType=”text” />

Steps to implement EditText in Kotlin

Step 1: Editing the activity_main.xml

Open activity_main.xml file and add the following code. Here we are adding an EditText and a Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:padding="64dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:autofillHints="name"
        android:inputType="text"
        android:hint="Enter your name..." />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/showInput"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_marginTop="32dp"
        android:text="Submit"
        android:textColor="@color/white"
        android:backgroundTint="@color/green"/>


</LinearLayout>

Layout:

Layout_EditText


Step 2: MainActivity File

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 onClick listener to the button

showButton.setOnClickListener {
//some functionalities...
}

Getting the text entered by user

val text = editText.text

Add the following code in the MainActivity.kt file.

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()
        }
    }
}

Expected Output:

Screenshot_20250118_114105

Some Important attributes of EditText

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.

GeekTip: Enhance your EditText by adding a text change listener in kotlin. For eg:

editText.doOnTextChanged { text, _, _, _ ->
if (text.isNullOrEmpty()) {
editText.error = “Input can not be empty!”
}
}



Next Article

Similar Reads

three90RightbarBannerImg