Working With the EditText in Android (original) (raw)

Last Updated : 23 Jul, 2025

EditText is one of the basic UI widgets, which is used to take the input from the user. The EditText is derived or is the extension of the TextView in Android. This article its been discussed in detail about the EditText in Android. The article also contains some of the redirections to other articles, also refer to them to get the detailed perspective of the EditText widget in Android. Have a look at the following list to get an idea of the overall discussion.

  1. Input Type for the EditText
  2. Getting the data or retrieving the data entered by the user
  3. Input Data Customization
  4. Adding hints for the placeholder
  5. Change the stroke color
  6. Change the highlighted color inside the EditText
  7. Event Listener for the EditText
  8. Error Message for the EditText filed
  9. Implementing Password visibility toggle
  10. Character counting using Material Design EditText

The Detailed Perspective of EditText in Android

Step 1: Create an empty activity project

Step 2: Working with the activity_main.xml file

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.321" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:backgroundTint="@color/green_500"
    android:text="SUBMIT"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:backgroundTint="@color/green_500"
    android:text="CANCEL"
    android:textColor="@color/green_500"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

`

Output UI:

Now let's discuss the various attributes of the EditText

1. Input Type for the EditText

InputType Attribute Type of the Data which is entered
number Mathematical numeric value
phone Contact Number based on the country code
date To take the date input
time To take the time is neededinput
textCapCharacters To take the entire input in the upper case letters
textMultiLine Makes the user input multiple lines of text
textEmailAddress To take the email address from the user
textPersonName To take the name of the person as input
textPassword To take the text password from the user, which turns to asterisks dots after entering the data
numberPassword To take only the numerical digits as a password
textVisiblePassword To take the text password from the user, which do not turns to asterisks dots after entering the data
textUri To take the particular URL of the website

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<!--the values mentioned in the table are to be 
    invoked inside the inputType attribute-->
<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
          
    android:inputType="phone"
          
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.321" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:backgroundTint="@color/green_500"
    android:text="SUBMIT"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:backgroundTint="@color/green_500"
    android:text="CANCEL"
    android:textColor="@color/green_500"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

`

Output:

2. Getting the data or retrieving the data entered by the user

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.321" />

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:backgroundTint="@color/green_500"
    android:text="SUBMIT"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    android:id="@+id/cancelButton"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:backgroundTint="@color/green_500"
    android:text="CANCEL"
    android:textColor="@color/green_500"
    app:layout_constraintEnd_toStartOf="@+id/submitButton"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

`

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.Toast

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)

    // register editText with instance
    val editText: EditText = findViewById(R.id.editText)

    // also register the submit button with the appropriate id
    val submitButton: Button = findViewById(R.id.submitButton)

    // handle the button with the onClickListener
    submitButton.setOnClickListener {

        // get the data with the "editText.text.toString()"
        val enteredData: String = editText.text.toString()

        // check whether the retrieved data is empty or not
        // based on the emptiness provide the Toast Message
        if (enteredData.isEmpty()) {
            Toast.makeText(applicationContext, "Please Enter the Data", Toast.LENGTH_SHORT)
                .show()
        } else {
            Toast.makeText(applicationContext, enteredData, Toast.LENGTH_SHORT).show()
        }
    }
}

}

Java

import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText editText;
Button submitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // register editText with instance
    editText = (EditText) findViewById(R.id.editText);

    // also register the submit button with the appropriate id
    submitButton = (Button) findViewById(R.id.submitButton);

    // handle the button with the onClickListener
    submitButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    
                    // get the data with the
                    // "editText.text.toString()"
                    String enteredData = editText.getText().toString();

                    // check whether the retrieved data is
                    // empty or not based on the emptiness
                    // provide the Toast Message
                    if (enteredData.isEmpty()) {
                        Toast.makeText(getApplicationContext(), "Please Enter the Data", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), enteredData, Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

}

`

Output:

3. Input Data Customization

  1. digits="56"
  2. inputType="number"
  1. maxLength="6"
  1. lines="1"
  2. maxLines="1"

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
          
    android:inputType="numberPassword"
    android:maxLength="6"
          
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.321" />

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:backgroundTint="@color/green_500"
    android:text="SUBMIT"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    android:id="@+id/cancelButton"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:backgroundTint="@color/green_500"
    android:text="CANCEL"
    android:textColor="@color/green_500"
    app:layout_constraintEnd_toStartOf="@+id/submitButton"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

`

Output:

4. Adding hints for the placeholder

The attribute which is used to provide the hint text for the EditText is:

android:hint="First and Last Name"

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
          
    android:hint="First and Last Name"
          
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.321" />

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:backgroundTint="@color/green_500"
    android:text="SUBMIT"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    android:id="@+id/cancelButton"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:backgroundTint="@color/green_500"
    android:text="CANCEL"
    android:textColor="@color/green_500"
    app:layout_constraintEnd_toStartOf="@+id/submitButton"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

`

Output:

5. Change the stroke color

android:backgroundTint="colorValue"

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    
    android:backgroundTint="@android:color/holo_red_dark"
    
    android:hint="First and Last Name"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.321" />

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:backgroundTint="@color/green_500"
    android:text="SUBMIT"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    android:id="@+id/cancelButton"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:backgroundTint="@color/green_500"
    android:text="CANCEL"
    android:textColor="@color/green_500"
    app:layout_constraintEnd_toStartOf="@+id/submitButton"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

`

Output:

6. Change the highlighted color inside the EditText

android:textColorHighlight="colorValue"

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"

    android:textColorHighlight="@android:color/holo_orange_light"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.321" />

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:backgroundTint="@color/green_500"
    android:text="SUBMIT"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
    android:id="@+id/cancelButton"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:backgroundTint="@color/green_500"
    android:text="CANCEL"
    android:textColor="@color/green_500"
    app:layout_constraintEnd_toStartOf="@+id/submitButton"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

`

Output:

7. Event Listener for the EditText

8. Error Message for the EditText filed

9. Implementing Password visibility toggle

10. Character counting using Material Design EditText

implementation 'com.google.android.material:material:1.2.1'

<androidx.constraintlayout.widget.ConstraintLayout 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" tools:ignore="HardcodedText">

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="16dp"
    android:hint="Enter Something"

    app:counterEnabled="true"
    app:counterMaxLength="6"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

`

Output: