How to add a drawable at the right end of a "TextInputLayout" dynamically?

Viewed 3678

I am trying to make something like this example:

Screenshot of a user name text field with a check suffix icon

I want the drawable to show when I verify the user name from the server.

I am using Material Design Text Input Field & EditText. Yes, I can do it with a simple EditText and an ImageView, but I want to use the standard elements.

I have looked at the official documentation, there is a way to add the image at the Right corner using XML, but I want to add it programmatically.

2 Answers

Use the setEndIconDrawable method:

    textInputLayout.setEndIconDrawable(R.drawable.xxxx);
    textInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);

or in a layout:

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

enter image description here

More info in the official doc.

You need to to set your drawable programmatically like below

inputTextEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context,R.drawable.drawableRight), null)
Related