Search code examples
androidandroid-edittextandroid-textinputlayoutandroid-textinputedittext

Setting on click listener to TextInputEditText drawable Right/End android studio


I am having a Text Input Layout with Text Input Edit Text inside. The Text Input Edit Text has a Drawable at the end. What i want to achieve is make the drawable at the end do something when it is clicked for example show a Toast message

Below is my XML Code


<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/til_toast"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:padding="3dp">

            <!--android:maxLength="13"-->
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/et_toast"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableEnd="@drawable/ic_calendar"
                android:drawableRight="@drawable/ic_calendar"
                android:hint="@string/date_from"
                android:inputType="date" />

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

How can i achieve this


Solution

  • Don't use android:drawableRight or android:drawableEnd in the TextInputEditText.

    Instead you can use:

    <com.google.android.material.textfield.TextInputLayout
        ...
        app:endIconMode="custom"
        app:endIconDrawable="@drawable/..."
    

    and then use the endIconOnClickListener:

    textInputLayout.setEndIconOnClickListener {
      // Respond to end icon presses
    }