Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
Dynamic EditText in Kotlin
Next article icon

Android EditText in Kotlin

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

html
<?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.

java
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
Dynamic EditText in Kotlin
author
aman neekhara
Improve
Article Tags :
  • Android
  • Android-View
  • Kotlin Android

Similar Reads

  • Kotlin Android Tutorial
    Kotlin is a cross-platform programming language that may be used as an alternative to Java for Android App Development. Kotlin is an easy language so that you can create powerful applications immediately. Kotlin is much simpler for beginners to try as compared to Java, and this Kotlin Android Tutori
    6 min read
  • TextView

    • TextView in Kotlin
      Android TextView is simply a view that are used to display the text to the user and optionally allow us to modify or edit it. First of all, open Kotlin project in Android Studio. Following steps are used to create Steps to Implement TextViewSteps by Step implementation for creating an application wh
      3 min read

    • Dynamic TextView in Kotlin
      Android TextView is an user interface that is used to display some text to the user. In this article we will be discussing how to programmatically create a TextView in Kotlin . Step by Step ImplementationStep 1: Create a new projectLet’s start by first creating a project in Android Studio. To do so,
      2 min read

    • AutoCompleteTextView in Kotlin
      Android AutoCompleteTextView is an advanced EditText which shows a list of suggestions when user starts typing text. When the user starts typing, a drop-down menu appears, showing a list of relevant suggestions based on the user input. The user can then select an item from the list. The AutoComplete
      3 min read

    • Dynamic AutoCompleteTextView in Kotlin
      Android AutoCompleteTextView is an advanced EditText which shows a list of suggestions when user starts typing text. When the user starts typing, a drop-down menu appears, showing a list of relevant suggestions based on the user input. The user can then select an item from the list. The AutoComplete
      4 min read

    • CheckedTextView in Kotlin
      CheckedTextView is an extension of TextView in Android that includes a checkmark, making it function like a checkbox. It is commonly used in list views where items can be selected or toggled between checked and unchecked states. Users can tap the text to change its checked status, and the checkmark
      2 min read

    • Dynamic CheckedTextView in Kotlin
      CheckedTextView is an extension of TextView in Android that includes a checkmark, making it function like a checkbox. It is commonly used in list views where items can be selected or toggled between checked and unchecked states. Users can tap the text to change its checked status, and the checkmark
      2 min read

    ScrollView

    • HorizontalScrollView in Kotlin
      ScrollView in Android allows multiple views that are places within the parent view group to be scrolled. Scrolling in the android application can be done in two ways either Vertically or Horizontally. In this article, we will be discussing how to create a Horizontal ScrollView in Kotlin. Some Import
      2 min read

    • Dynamic ScrollView in Kotlin
      In Android, ScrollView incorporates multiple views within itself and allows them to be scrolled. In this article we will be discussing how to programmatically create a Scroll view in Kotlin. Step by Step ImplementationStep 1: Create a new projectTo create a new project in Android Studio please refer
      2 min read

    • Dynamic Horizontal ScrollView in Kotlin
      Android ScrollView allows multiple views that are places within the parent view group to be scrolled. Scrolling in the android application can be done in two ways either Vertically or Horizontally. In this article, we will be discussing how to programmatically create a Horizontal ScrollView in Kotli
      2 min read

    ImageView

    • Dynamic ImageView in Kotlin
      An ImageView as the name suggests is used to display images in Android Applications. In this article, we will be discussing how to create an ImageView programmatically in Kotlin. Step by Step ImplementationStep 1: Create a new projectTo create a new project in Android Studio please refer to How to C
      2 min read

    ListView

    • Android ListView in Kotlin
      ListView in Android is a ViewGroup which is used to display a scrollable list of items arranged in multiple rows. It is attached to an adapter which dynamically inserts the items into the list. The main purpose of the adapter is to retrieve data from an array or a database and efficiently push every
      3 min read

    Button

    • Button in Android
      In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it. This article demonstrates how to create a button in Android Studio. Class Hierarchy of the Button Class in Kotlinkotlin
      3 min read

    • ImageButton in Kotlin
      Android ImageButton is a user interface widget which is used to display a button having image and to perform exactly like button when we click on it but here, we add an image on Image button instead of text. There are different types of buttons available in android like ImageButton, ToggleButton, et
      3 min read

    • Dynamic ImageButton in Kotlin
      An ImageButton in Android is a specialized Button that displays an image instead of text while functioning like a regular button. When clicked, it triggers an action just like a standard button. Android provides various button types, including ImageButton, ToggleButton, and more. To set an image on
      2 min read

    • RadioButton in Kotlin
      Android Radio Button is bi-state button which can either be checked or unchecked. Also, it's working is same as Checkbox except that radio button can not allow to be unchecked once it was selected. Generally, we use RadioButton controls to allow users to select one option from multiple options. By d
      4 min read

    • Dynamic RadioButton in Kotlin
      An Android RadioButton is a bi-state button that can be either checked or unchecked. It functions similarly to a CheckBox, but with one key difference: once selected, a RadioButton cannot be unchecked by tapping it again. RadioButtons are typically used within a RadioGroup to allow users to select o
      2 min read

    EditText

    • Android EditText in Kotlin
      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 valida
      3 min read

    • Dynamic EditText in Kotlin
      EditText is a commonly used UI component in Android for getting user input, especially in login and registration screens. While we have already learned how to create an EditText using XML layouts, this article will focus on how to create an EditText programmatically in Kotlin. Step by Step Implement
      2 min read

    Layouts

    • Android UI Layouts
      Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activities—each representing a separate screen—every activity has multip
      5 min read

    • Android TableLayout in Kotlin
      TableLayout in Android is a ViewGroup subclass that is designed to align child views in rows and columns like a grid structure. It automatically arranges all the child elements into rows and columns without displaying any border lines between cells. The Table Layout's functionality is almost similar
      4 min read

    • FrameLayout in Android
      Android Framelayout is a ViewGroup subclass that is used to specify the position of multiple views placed on top of each other to represent a single view screen. Generally, we can say FrameLayout simply blocks a particular area on the screen to display a single view. Here, all the child views or ele
      3 min read

    • Android RelativeLayout in Kotlin
      RelativeLayout in Android is a ViewGroup subclass, that allows users to position child views relative to each other (e.g., view A to the right of view B) or relative to the parent (e.g., aligned to the top of the parent). Instead of using LinearLayout, we have to use RelativeLayout to design the use
      4 min read

    • Android LinearLayout in Kotlin
      LinearLayout in Android is a ViewGroup subclass, used to arrange child view elements one by one in a singular direction either horizontally or vertically based on the orientation attribute. We can specify the linear layout orientation using the android:orientation attribute. All the child elements a
      2 min read

    Bar

    • SeekBar in Kotlin
      SeekBar in Android is a modified version of progressBar that has a draggable thumb in which a user can drag the thumb back and forth to set the current progress value. We can use SeekBar in our Android Devices like Brightness control, volume control etc. It is one of the important user interface ele
      3 min read

    • Dynamic SeekBar in Kotlin
      SeekBar in Android is a modified version of progressBar that has a draggable thumb in which a user can drag the thumb back and forth to set the current progress value. We can use SeekBar in our Android Devices like Brightness control, volume control etc. It is one of the important user interface ele
      2 min read

    • Discrete SeekBar in Kotlin
      In Android Discrete SeekBar is just an advancement of progressBar just like the SeekBar, the only difference in SeekBar and discrete SeekBar being that in discrete SeekBar, we can only set the value only to discrete values like 1, 2, 3, and so on. In this article, we will be discussing how to create
      2 min read

    • RatingBar in Kotlin
      Android RatingBar is a user interface widget which is used to get the rating from the customers or users. It is an extension of SeekBar and ProgressBar that shows star ratings and it allow users to give the rating by clicking on the stars. In RatingBar, we can set the step size using android:stepSiz
      3 min read

    • Dynamic RatingBar in Kotlin
      Android RatingBar is a user interface widget which is used to get the rating from the customers or users. It is an extension of SeekBar and Progress Bar that shows star ratings and it allow users to give the rating by clicking on the stars. In RatingBar, we can set the step size using android:stepSi
      3 min read

    • ProgressBar in Kotlin
      Android ProgressBar is a user interface control that indicates the progress of an operation. For example, downloading a file, uploading a file on the internet we can see the progress bar to estimate the time remaining in operation. There are two modes of ProgressBarDeterminate ProgressBarIndetermina
      3 min read

    • Dynamic ProgressBar in Kotlin
      Android ProgressBar is user interface control that is used to show some kind of progress. For instance, loading of some page, downloading of some file or waiting for some event to complete. In this article we will be discussing how to programmatically create a progress bar in Kotlin. Step by Step Im
      2 min read

    Switcher

    • Switch in Kotlin
      Android Switch is a two-state user interface element that is used to toggle between ON and OFF similar to a button. By tapping the button we can drag it back and forth to make it either ON or OFF. The Switch element is useful when there are only two states required for an activity like to either cho
      4 min read

    • Dynamic Switch in Kotlin
      Android Switch is also a two-state user interface element that is used to toggle between ON and OFF as a button. By touching the button we can drag it back and forth to make it either ON or OFF. The Switch element is useful when only two states require for activity either choose ON or OFF. We can ad
      3 min read

    • TextSwitcher in Kotlin
      Android TextSwitcher is a user interface widget that contains a number of textView and displays one at a time. Textswitcher is a subclass of View Switcher which is used to animate one text and display the next one. Generally, we use TextSwitcher in two ways manually in XML layout and programmaticall
      3 min read

    • Dynamic TextSwitcher in Kotlin
      Android TextSwitcher is a user interface widget that contains number of textView and displays one at a time. Textswitcher is subclass of View Switcher which is used to animates one text and displays next one. Here, we create TextSwitcher programmatically in Kotlin file. Steps of Implementing Dynamic
      2 min read

    • ImageSwitcher in Kotlin
      Android ImageSwitcher is a user interface widget that provides a smooth transition animation effect to the images while switching between them to display in the view. ImageSwitcher is subclass of View Switcher which is used to animates one image and displays next one. Generally, we use ImageSwitcher
      3 min read

    • Dynamic ImageSwitcher in Kotlin
      Android ImageSwitcher is a user interface widget that provides a smooth transition animation effect to the images while switching between them to display in the view. ImageSwitcher is subclass of View Switcher which is used to animates one image and displays next one. Here, we create ImageSwitcher p
      3 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences