The Wayback Machine - https://web.archive.org/web/20240621035410/https://www.geeksforgeeks.org/edittext-widget-in-android-using-java-with-examples/
Open In App

EditText widget in Android using Java with Examples

Last Updated : 24 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
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 textfield 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

Syntax: 

<SomeLayout>
    .
    .
    <Edittext
        android:SomeAttribute1 = "Value of attribute1"
        android:SomeAttribute2 = "Value of attribute2"
        .
        .
        android:SomeAttributeN = "Value of attributeN"/>
    .
    .
</SomeLayout>

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: 

 <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"
      android:gravity="center">>

    <Edittext
        android:id="@+id/text_view_id"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="GeeksforGeeks" />

    <Button
        android:id="@+id/button_id"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_below="@+id/edittext_id"
        android:layout_marginTop="20dp"
        android:text="Submit"
        android:textColor="#fff"
        android:background="@color/colorPrimary"/>

 </RelativeLayout>

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: 
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

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

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

Below is the implementation of the above approach: 

Filename: activity_main.xml

XML




<?xml version="1.0" encoding="utf-8"?>
 
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">
 
    <EditText
        android:id="@+id/edittext_id"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:hint="Enter your Name"/>
 
    <Button
        android:id="@+id/button_id"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_below="@+id/edittext_id"
        android:layout_marginTop="20dp"
        android:text="Submit"
        android:textColor="#fff"
        android:background="@color/colorPrimary"/>
 
</RelativeLayout>


Filename: MainActivity.java

Java




package com.project.edittext;
 
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    private EditText editText;
    private Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText
            = (EditText)findViewById(R.id.edittext_id);
        button
            = (Button)findViewById(R.id.button_id);
 
        button.setOnClickListener(
            new View.OnClickListener() {
 
                @Override
                public void onClick(View v)
                {
                    String name
                        = editText.getText()
                              .toString();
                    Toast.makeText(MainActivity.this,
                                   "Welcome to GeeksforGeeks "
                                       + name,
                                   Toast.LENGTH_SHORT)
                        .show();
                }
            });
    }
}


Output: Upon starting of the App and entering the name in EditText. The name in the EditText is then displayed: 

XML Attributes of Edittext in Android

Attributes Description
android:id Used to uniquely identify the control
android:gravity Used to specify how to align the text like left, right, center, top, etc.
android:hint Used to display the hint text when text is empty
android:text Used to set the text of the EditText
android:textSize Used to set size of the text.
android:textColor Used to set color of the text.
android:textStyle Used to set style of the text. For example, bold, italic, bolditalic, etc.
android:textAllCaps Used this attribute to show the text in capital letters.
android:width It makes the TextView be exactly this many pixels wide.
android:height It makes the TextView be exactly this many pixels tall.
android:maxWidth Used to make the TextView be at most this many pixels wide.
android:minWidth Used to make the TextView be at least this many pixels wide.
android:background Used to set background to this View.
android:backgroundTint Used to set tint to the background of this view.
android:clickable Used to set true when you want to make this View clickable. Otherwise, set false.
android:drawableBottom Used to set drawable to bottom of the text in this view.
android:drawableEnd Used to set drawable to end of the text in this view.
android:drawableLeft Used to set drawable to left of the text in this view.
android:drawablePadding Used to set padding to drawable of the view.
android:drawableRight Used to set drawable to right of the text in this view.
android:drawableStart Used to set drawable to start of the text in this view.
android:drawableTop Used to set drawable to top of the text in this view.
android:elevation Used to set elevation to this view.

 



Similar Reads

EditText in Android using Jetpack Compose
EditText is one of the most important widget which is seen in most of the apps. This widget is generally use to get the data from users. Users can directly communicate with the app using this widget. This widget is used to get the data from user in the form of numbers, text or any other text. In this article we will take a look at the implementatio
5 min read
How to Search an Item in ListView using EditText and TextWatcher in Android?
Some applications provide a search bar for looking up any particular item from a list of items. Technically, a search bar is an EditText and an item list can be a ListView, RecyclerView, or a GridView that contains some items. Now, when a user types something in the EditText, the list must update in accordance with the text that the user types. Str
3 min read
Android EditText in Kotlin
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 OnClickListener for the button to get the user input from EditText
3 min read
How to Add Mask to an EditText in Android?
EditText is an android widget. It is a User Interface element used for entering and modifying data. It returns data in String format. Masking refers to the process of putting something in place of something else. Therefore by Masking an EditText, the blank space is replaced with some default text, known as Mask. This mask gets removed as soon as th
3 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 its been discussed to implement the special type of text fields, those are called Material Design EditText. Have a look at the
5 min read
Theming of Material Design EditText in Android with Example
Prerequisite: Material Design EditText in Android with Examples In the previous article Material Design EditText in Android with Examples Material design Text Fields offers more customization and makes the user experience better than the normal text fields. For example, Icon to clear the entire EditText field, helper text, etc. In this article, it'
4 min read
Key Properties of Material Design EditText in Android
In the previous article Material Design EditText in Android with Examples its been discussed how to implement the Material design EditText and how they differ from the normal EditText. In this article its been discussed on how to customize MDC edit text fields with their key properties. Have a look at the following image to get the idea about the k
4 min read
Implement Form Validation (Error to EditText) in Android
Many of the already existing Android applications when it comes to the forms include user details. If the user enters the wrong information into the text fields or if the user leaves the text fields without filling them, there is a need to provide certain alert information to the TextFields which are unfilled and which contain the wrong information
6 min read
How to Customize AppCompat EditText in Android?
The EditText is one of the important UI element which takes the data as input from the user. The usual EditText in Android which looks decent enough has only the hint text and line which makes the user click on that line and insert the data. Refer to the article EditText widget in Android using Java with Examples, which explains basics about the no
4 min read
Working With the EditText in Android
EditText is one of the basic UI widgets, which is used to take the input from the user. The EditText is derived or is the extension of the TextView in Android. This article its been discussed in detail about the EditText in Android. The article also contains some of the redirections to other articles, also refer to them to get the detailed perspect
9 min read