Android EditText

In Android, EditText is a UI widget utilized to allow the user to input or change the text. While using the EditTextwidget in your Android application, you need to use the inputType property to specify the type of data your text field can accept.

For example, if you accept plain text, you must specify inputType as "text". If the EditText field is anemail, you must specify the inputType as "textEmailAddress".

TheEditTextwidget add-on is an extended version of the TextView, utilized to allow the user to input values.

In android, you can add an EditTextwidget in two ways in an XML layout file, or programmatically add it from an activity file.

Adding anEditText in Layout File

Here is the simple way to declare EditText widget in XML layout file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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/constraint"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Javatpoint.EditTextActivity">


<EditText
android:id="@+id/editTextExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Here we defined the EditText widget to accept Person Name text by specifyinginputType as “textPersonName” in the XML layout file.

AddEditText in Activity Java File

In android, we can create an EditText widget programmatically in an activity file to allow users to enter text based on our requirements. 

Here is an example of addingEditTextwidgetdynamically in an activity.java file.

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;


import android.os.Bundle;
import android.widget.EditText;


import com.example.myapplication.R;


public class EditTextActivityextends AppCompatActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
ConstraintLayoutconstraintLayout= findViewById(R.id.constraint);
EditTextetex= new EditText(this);
etex.setHint("Name");
constraintLayout.addView(etex);


    }
}

Setting the text of EditText

In Android, the text of the EditText widget can be declared in a layout file or set using the javasetText() method.

Here is an illustration of setting the text of a EditText widget while declaring it in an XML layout file.

<EditText
android:id="@+id/wtp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Paradise" />

We have used android:text property to the set required text for EditText widget in XML Layout file.

Here is another way for setting the text of EditText widget programmatically in javafile using setText() method.

EditText ex = findViewById(R.id.editTextExample);
ex.setText("Welcome to Paradise");

Here, by using findViewbyId() we create an instance of the EditText widget which we declared in the XML layout file, and set the text using the setText() method.

Getting Text of EditText

We can get the text of EditText widget by using getText() method in activity.java file in Android.

Here is anillustration to get the content of EditText widget in java fileusing getText() method.

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;


import android.os.Bundle;
import android.widget.EditText;


import com.example.myapplication.R;


public class EditTextActivityextends AppCompatActivity{
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
ConstraintLayoutconstraintLayout= findViewById(R.id.constraint);
EditTextetEx= findViewById(R.id.editTextExample);
name = etEx.getText().toString();


    }
}

Here, by using findViewbyId() we create an instance of theEditText widget which we defined in the XML layout file, and get the text of the EditText widget using the getText() method.

EditText Attributes

Here are some of the commonly used attributes related to the EditTextwidget:

AttributeDescription
android:idUtilized to uniquely identify each EditText.
android:gravityUtilized to specify how to align the text like left, right, center, top, etc.
android:textUtilized to set the text.
android:hintUtilized to show the hint text when EditText is empty.
android:textColorUtilized to modify the text’s color.
android:textColorHintUtilized to modify the color of hint text.
android:textSizeUtilized to declare the text’s size.
android:textStyleUtilized to modify text style (bold, italic).
android:backgroundUtilized to set EditText background color.
android:emsUtilized to make the EditTextgiven ems wide.
android:widthMakes the EditTextgiven pixels wide.
android:heightMakes the EditText given pixels high.
android:maxWidthUtilizedto make the EditText at most given pixelwide.
android:minWidthUtilizedto make the EditText at least given pixel wide.
android:textAllCapsUtilized to make the whole text Capital.
android:typefaceUtilizedto define the text’s Typeface (normal, sans, serif).
android:textColorHighlightUtilizedto modify the text selection highlightcolor.
android:inputTypeUtilizedto define the text’s type being entered in EditText.
android:fontFamilyUtilizedto define the text’s font family.
android:editableIf we set true, EditText will allow us to input or change the text.

Related Topics

Android Broadcast Receivers

Android applications can send or receive broadcast messages from the Android operating system and other Androidapplications, like the publish-subscribe design pattern. When an event of interest happens,these broadcasts are sent. For...

4 minutes read.

Android TextView

Widgets are user interface (UI) elements that help users to interact with Android applications. It is one of many equipments that can be used to improvise the user interface of...

4 minutes read.

Android EditText

In Android, EditText is a UI widget utilized to allow the user to input or change the text. While using the EditTextwidget in your Android application, you need to use...

4 minutes read.

Android Studio's Layout Editor

The Layout Editor allows you to efficiently create layouts by dragging UI elements from the Palette into the visual design editor instead of manually creating the layout XML file by...

8 minutes read.

Android Date picker example

How to pick date from the user in Android? Write an android example to pick date from the user and display it. Android provides control for the user to pick a date...

3 minutes read.

Android Activity

What is an Activity? Android Activity is a screen in the user interface of an Android application. In this way, Android activity is very similar to the desktop application window. An...

4 minutes read.

Event Handling in Android

Events are a convenient way to gather data about user interactions with interactive elements of your application. Like a button click or a screen touch. A queue of events on...

3 minutes read.

How to show image on an android application

ImageView Widget in Android What you should already KNOW? You should be familiar with: 1) Creating, building, and running apps in Android Studio 2) Basics of android widgets   Explanation: Image View widget is used...

2 minutes read.

How to reduce the size of APK in Android

What exactly is an APK file? The full form of APK is Android Package Kit. It is the Android operating system's application file format. We use Android Studio to generate APK...

7 minutes read.

Android ListView

A kind of AdapterView that displays a vertical list of scrollable views, each of which is laid out below each other is called ListView. Using adapters, items are inserted into...

4 minutes read.

How to insert an email in android application in android version 8

Email Address Insertion It is used to insert email adrress from the user in the android application.The only point of difference between an EditText and an Emailtext is that EmailText consisits...

1 minute read.

How to crop Image from Camera and Gallery in Android?

In this tutorial, we'll learn how to crop photos in the Android Gallery and Camera. There is no crop feature in this project, contrary to what was covered in the...

5 minutes read.

Android 9.0 Pie vs. Android 10, which one is better

Android smartphone users are always searching for new system upgrades and keep an eye on system updates whenever the next version is published. Android 9 Pie and Android Q 10 are...

5 minutes read.

How to Update Gradle in Android Studio?

The Android Gradle plugin adds a number of features that are unique to develop Android apps to the Gradle build system, which is the foundation of the Android Studio development...

2 minutes read.

Different Ways to Create aar File in Android Studio

A file called an Android archive file (*.aar) can be created using Android Studio and comprises classes and methods that use related files and classes for Android. The Android library...

4 minutes read.

Android Time picker example

How to pick Time from the user in android? Write an android example to pick Time from the user and display the date to the user. Android provides controls for the user...

3 minutes read.

Jetpack Architecture Components in Android

A collection of software elements, libraries, tools, and instructions is called Android Jetpack. It is available to assist in creating reliable Android applications. Jetpack, which was introduced by Google in...

8 minutes read.

How to Generate QRCode in Android?

Many apps use QR codes to show data in a way that is machine-readable. Data is represented using these codes in a secure manner such that only computers, not humans,...

4 minutes read.

Top Programming Languages for Android Development

For mobile apps, there are so many programming languages. You would need to spend days simply researching if you had to pick one from a list of 30 coding languages...

8 minutes read.

Android Tutorial

This Android tutorial will provide you with all the concepts that will be beneficial for both beginners and advanced-level students or candidates. You can learn this tutorial to begin your...

6 minutes read.