1

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.

Mohamed Jihed Jaouadi
  • 1,427
  • 4
  • 25
  • 44

3 Answers3

0

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)
Dinesh
  • 1,410
  • 2
  • 16
  • 29
0

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Not working .. !! The "setEndIconMode" & "setEndIconDrawable" functions does not exist any more ! – Mohamed Jihed Jaouadi Jul 27 '20 at 08:48
  • @J.M.J They **exist** in [1.1.0](https://github.com/material-components/material-components-android/blob/1.1.0/lib/java/com/google/android/material/textfield/TextInputLayout.java#L2374) in [1.2.0-rc01](https://github.com/material-components/material-components-android/blob/1.2.0-rc01/lib/java/com/google/android/material/textfield/TextInputLayout.java#L3013) and [1.3.0-alpha02](https://github.com/material-components/material-components-android/blob/1.3.0-alpha02/lib/java/com/google/android/material/textfield/TextInputLayout.java#L3022). Which version are you using? – Gabriele Mariotti Jul 27 '20 at 08:57
-3

Kindly use the following kotlin line for this purpose

yourInputLayout.error = "Please enter valid email address"
Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46