Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
EditText widget in Android with Example
Next article icon

EditText widget in Android with Example

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Widget refers to the elements of the UI (User Interface) that helps user interacts with the Android App. EditText is one of many such widgets which can be used to retrieve text data from user.

EditText refers to the widget that displays an empty text field in which a user can enter the required text and this text is further used inside our application.

Class Syntax: 

public class EditText extends TextView

Class Hierarchy: 

java.lang.Object
↳android.view.View
↳ android.widget.TextView
↳ android.widget.EditText


Here the layout can be any layout like Relative, Linear, etc (Refer this article to learn more about layouts). And the attributes can be many among the table given below in this article.

Example: 

XML
<?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"     tools:context=".MainActivity">      <EditText         android:id="@+id/edit_text"         android:layout_height="wrap_content"         android:layout_width="match_parent"         android:text="GeeksForGeeks" /> </LinearLayout> 


How to include a EditText in an Android App?

  • First of all, Create a new Android app, or take an existing app to edit it. In both the case, there must be an XML layout activity file and a Java class file linked to this activity.
  • Open the Activity file and include a EditText field in the layout (activity_main.xml) file of the activity and also add a Button in activity_main.xml file too.
  • Now in the Java file, link this layout file with the below code: 
MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main); } 
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {     super.onCreate(savedInstanceState)     setContentView(R.layout.activity_main) } 


where activity_main.xml is the name of the layout file to be attached.

  • Now we will add code in MainActivity file to make our layout interactive or responsive. Our application will generate a toast on clicking the button with the text as entered by user.
  • The complete code of the layout file and the Java/Kotlin file is given below.

Below is the implementation of the above approach: 

activity_main.xml:

activity_main.xml
<?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/text_view_id"         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/button_id"         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> 

Here's the code for the MainActivity:

Java
package org.geeksforgeeks.demo;  import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      // Declare UI elements: EditText for user input and Button for submission     private EditText editText;     private Button button;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Link EditText and Button to their UI elements         editText = findViewById(R.id.text_view_id);         button = findViewById(R.id.button_id);          // Set OnClickListener for the button         button.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 String name = editText.getText().toString();                  // Display a Toast message showing the input text                 Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();             }         });     } } 
Kotlin
package org.geeksforgeeks.demo  import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.Toast import androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {      // Declare UI elements: EditText for user input and Button for submission     private lateinit var editText: EditText     private lateinit var button: Button      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // Link the EditText and Button variables to their UI elements in activity_main.xml         editText = findViewById(R.id.text_view_id);         button = findViewById(R.id.button_id);          // Set up an onClickListener for the button to handle click events         button.setOnClickListener {             val name = editText.text.toString();              // Display a short message (Toast) showing a welcome message with the user's input             Toast.makeText(this, name, Toast.LENGTH_SHORT).show()         }     } } 


Output:

Upon starting of the App, entering a name in EditText and pressing the "Submit" button, the name in the EditText is then displayed as a Toast: 

Output_Application

For learning more about the topic refer to these articles:

  • Working With the EditText in Android
  • How to add Mask to an EditText in Android

Some Important XML Attributes of EditText in Android

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.


 


Next Article
EditText widget in Android with Example

R

RahulSabharwal
Improve
Article Tags :
  • Android
  • Kotlin Android
  • Android-View
  • Java-Android

Similar Reads

    Context Menu in Android with Example
    In Android, there are three types of menus available to define a set of options and actions in the Android apps. Here in this article let's discuss the detail of the Context Menu. In Android, the context menu is like a floating menu and arises when the user has long-pressed or clicked on an item and
    4 min read
    Material Design EditText in Android with Example
    EditText is one of the important UI elements. Edittext refers to the widget that displays an empty text field in which a user can enter the required text and this text is further used inside the application. In this article it has been discussed to implement the special type of text fields, those ar
    3 min read
    TextView widget in Android with Examples
    Widget refers to the elements of the UI (User Interface) that help the user interact with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app. TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc
    5 min read
    CardView in Android With Example
    CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI
    3 min read
    Spinner in Android with Example
    Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
    4 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