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:
Attribute | Description |
android:id | Utilized to uniquely identify each EditText. |
android:gravity | Utilized to specify how to align the text like left, right, center, top, etc. |
android:text | Utilized to set the text. |
android:hint | Utilized to show the hint text when EditText is empty. |
android:textColor | Utilized to modify the text’s color. |
android:textColorHint | Utilized to modify the color of hint text. |
android:textSize | Utilized to declare the text’s size. |
android:textStyle | Utilized to modify text style (bold, italic). |
android:background | Utilized to set EditText background color. |
android:ems | Utilized to make the EditTextgiven ems wide. |
android:width | Makes the EditTextgiven pixels wide. |
android:height | Makes the EditText given pixels high. |
android:maxWidth | Utilizedto make the EditText at most given pixelwide. |
android:minWidth | Utilizedto make the EditText at least given pixel wide. |
android:textAllCaps | Utilized to make the whole text Capital. |
android:typeface | Utilizedto define the text’s Typeface (normal, sans, serif). |
android:textColorHighlight | Utilizedto modify the text selection highlightcolor. |
android:inputType | Utilizedto define the text’s type being entered in EditText. |
android:fontFamily | Utilizedto define the text’s font family. |
android:editable | If we set true, EditText will allow us to input or change the text. |