Search code examples
androidonclickandroid-edittextmaterial-components-androidandroid-textinputlayout

How can I click a drawable svg in TextinputEdittext?


I have an svg on an textinputedittext. It is on the right side of the edittext. I want to click and get an action from it.

This is xml of edittexxt:

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textinputlayout_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:endIconMode="custom"
                app:errorIconDrawable="@drawable/ic_icon_show_hide_svg"
                app:layout_constraintStart_toStartOf="@id/constraintlayout_login_editable_areas"
                app:layout_constraintEnd_toEndOf="@id/constraintlayout_login_editable_areas"
                app:layout_constraintTop_toBottomOf="@id/edittext_login_username"
                android:layout_marginTop="16dp">

                <com.example.android.custombutton.CustomEdittext
                    android:id="@+id/edittext_login_password"
                    android:layout_width="match_parent"
                    android:layout_height="54dp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:inputType="textPassword"
                    android:layout_marginTop="16dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"
                    android:paddingEnd="8dp"
                    android:hint="Password"/>

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

How can I click this: @drawable/ic_icon_show_hide_svg


Solution

  • Use:

    <com.google.android.material.textfield.TextInputLayout
           android:id="@+id/textinputlayout_login"
           app:endIconMode="custom"
           app:endIconDrawable="@drawable/ic_icon_show_hide_svg"
           ..>
    

    Then you can define a clickListener with:

    textinputlayout_login.setEndIconOnClickListener { 
        //do something 
    }
    

    For a password textfield you can also use the built-in feature endIconMode="password_toggle":

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