18

Does Android EditText has input type with clear button?

It should work like html 5 search input type.

enter image description here

Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197
  • 1
    possible duplicate of [How to create EditText with cross(x) button at end of it?](http://stackoverflow.com/questions/6355096/how-to-create-edittext-with-crossx-button-at-end-of-it) – yanchenko Jun 25 '13 at 23:12

3 Answers3

9

There is no internal input-type, but you can customize it to work so. This link might help you: Stack-Overflow: EditText with clear button

Community
  • 1
  • 1
SamSPICA
  • 1,366
  • 15
  • 31
8

You can use the following, it works quite well

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:endIconMode="clear_text"
    tools:hint="Label">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textInputEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Value" />

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

But this approach would not be valid for a MaterialAutoCompleteTextView

Sergio Sánchez Sánchez
  • 1,694
  • 3
  • 28
  • 48
7

If you're using TextInputLayout that you can use endIconMode and endIconDrawable

  • Does android.support.design.widget.TextInputLayout has this function, if not pls specify which TextInputLayout to use?? – KJEjava48 Apr 30 '21 at 07:17
  • @KJEjava48 use `com.google.android.material.textfield.TextInputLayout`. And icon you can set from `.xml`. Like, `app:endIconDrawable="@drawable/xxx`. – Ivan Krasilnikov Apr 30 '21 at 14:51
  • I have a datepicker edittext field where android:focusable="false" , how can i use this for such fields???pls help – KJEjava48 May 01 '21 at 07:33