AppCompatEditText

In Android, AppCompatEditText is a standard entry widget in android apps. It is an overlay over AppCompatTextView that configures itself to be editable. AppCompatEditText is a subclass of AppCompatTextView with text editing operations. We often use AppCompatEditText in our applications in order to provide an input or text field, especially in forms. The most simple example of AppCompatEditText is Login or Sign-in form or Sign-up form or filling any details form .

Class Hierarchy
public class AppCompatEditText extends EditText implements TintableBackgroundView
java.lang.Object
↳ android.view.View
  ↳ android.widget.TextView
    ↳ android.widget.EditText
      ↳ androidx.appcompat.widget.AppCompatEditText

We Recommend to use new UI components i.e. androidx components


Create AppCompatEditText in layout xml file

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/et1"    
    android:layout_width="match_parent"    
    android:layout_height="wrap_content"
    android:hint="Enter Name"/>


Create AppCompatEditText in programatically

AppCompatEditText editText = new AppCompatEditText(this);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(params);
editText.setHint("Enter name");


Customizing the Input Type

By default, any text contents within an AppCompatEditText control is displayed as plain text. By setting inputType, we can facilitate input of different types of information, like phone numbers and passwords:

<AppCompatEditText
    ...
    android:inputType="phone">
</AppCompatEditText>

Most common input types include:

type Description
textUri Text that will be used as a URI
textEmailAddress Text that will be used as an e-mail address
textPersonName Text that is the name of a person
textPassword Text that is a password that should be obscured
textVisiblePassword Text, next button, and no microphone input
number A numeric only field
phone For entering a phone number
date For entering a date
time For entering a time
textMultiLine Allow multiple lines of text in the field

You can set multiple inputType attributes if needed (separated by '|'), example are following with few inputTypes

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- inputType as person name -->
    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Entere Name"
        android:inputType="textPersonName"
        android:selectAllOnFocus="true" />

    <!-- inputType as numeric password -->
    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Numeric Password"
        android:inputType="numberPassword" />

    <!-- inputType as email address -->
    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Email address"
        android:inputType="textEmailAddress" />

    <!-- inputType as date -->
    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="dd/mm/yyyy"
        android:inputType="date" />

    <!-- inputType as phone number-->
    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Phone number"
        android:inputType="phone" />

</androidx.appcompat.widget.LinearLayoutCompat>

Now this is your decision or your requirements where you want to use AppCompatEditText, Happy Coding developers enjoy.