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

Button Widget in Android Tutorial

Explanation: Button is a widget under Buttons section which is used to execute specific code. Example: XML file: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">     <Button         android:id="@+id/button"         android:layout_width="wrap_content"    ...

1 minute read.

AdapterViewFlipper in Android with Example

The AdapterViewFlipper class, a subclass of the ViewAnimator class, is what we use to switch from two or multiple views when only one is present at a time. The AdapterViewFlipper...

10 minutes read.

Creating a Calendar View App in Android

This article demonstrates how to make an Android application that uses CalendarView to display the Calendar. It also provides the option to pick the current date and view the date....

3 minutes read.

How to insert the mobile number on an android application using EditText

Number Insertion in android application: Sometimes while dealing with numbers, we don't want to insert any string inside an number so, we premiliary define an EditText so we externally specify...

2 minutes read.

Android Styles and Themes

Android styles allow you to design the appearance (for example, colors, size, and fonts) of Android UI components from XML resource files. With this method, you only need to set...

5 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.

Top 7 Android App Development Books

Do you want to create Android apps but don't know where to start? One should first be familiar with the basics of Android before we discuss these books. With 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.

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.

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

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

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.

Android Navigation Drawer

A sliding menu used to show important links in the Android Application is called Navigation Drawer. The navigation drawermakes it easier to navigate between the fragments. It is not displayed...

5 minutes read.

What is Android Studio

Android Studio powered by the IntelliJ platform is an integrated development environment (IDE) and is officially recognized for developing androidapplications. Its foundation lies on JetBrains' IntelliJ IDEA and is specialized...

3 minutes read.

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 to insert the image...

2 minutes read.