How to show the error icon in a TextInputLayout programatically?

1.3k Views Asked by At

I'm using a TextInputLayout widget and I simply want to display the error icon in the end side of the view regradless of the error message.

3

There are 3 best solutions below

0
On

Kindly use the following kotlin line for this purpose

yourInputLayout.error = "Please enter valid email address"
0
On

If you want to change the error icon and display error use like

val errorDrawable = ContextCompat.getDrawable(context!!, R.drawable.ic_error)
input_layout.error = SpannableString("error message").apply {
                setSpan(ImageSpan(errorDrawable, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
2
On

If you want to show the error icon the only built-in method is to display a message error:

textInputLayout.setError("Error message");

If you want to display the error icon without an error message there is a workaround:

//Update the EndIcon with a custom icon error
textInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);
textInputLayout.setEndIconDrawable(R.drawable.error_icon);

//Tint the end icon with the error color defined in your app theme
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorError, typedValue, true);      
textInputLayout.setEndIconTintList(
         ContextCompat.getColorStateList(this,typedValue.resourceId));

enter image description here