Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

EditText widget in Android using Java with Examples

  • Last Updated : 24 Sep, 2021

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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

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.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!