Android Tutorial

Android Tutorial Android Toast Button Widget In Android Tutorial Imageview Widget In Android Android Date Picker Example Android Time Picker Example Android Operating System Android Activity Android Architecture Android Content Providers Android Edittext Android Emulator Android Fragments Android Gridview Android Broadcast Receivers Event Handling In Android What Is Android Studio Android Navigation Drawer Android Recyclerview Android Scrollview Android Navigation Android Operating System Android ListView Android Studio Resources Android Studio's Layout Editor Android TextView Android Styles and Themes How To Show Image On An Android Application How To Insert An Email In Android Application In Android Version 8 How To Insert The Mobile Number On An Android Application Using Edittext Difference between android developer and web AdapterViewFlipper in Android with Example Android 9.0 Pie vs. Android 10, which one is better Android Development or Web Development which one is the best to choose Android Project Folder Structure Assets folder in Android How to reduce the size of APK in Android Top 7 Android App Development Books Top Programming Languages for Android Development Android Navigation Application Components Difference between android developer and web developer Event Handling in Android How to reduce the size of APK in Android Top 7 Android App Development Books Top Programming Languages for Android Development AdapterViewFlipper in Android with Example Android 9.0 “Pie” vs. Android 10, which one is better Android Development or Web Development which one is the best to choose Android EditText Android Fragments How to Update Gradle in Android Studio Navigation Drawer ScrollView What is Android Studio? Android Architecture Android Content Providers

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.