11

I've found an issue while creating a login form. I show some errors on my TextInputLayout and disable them, when the user filled in something correctly.

error example

I set it with

mTextInputLayout.setError("This field is required");

and disable it with

mTextInputLayout.setError(null);

Problem is that there are still paddings of the empty TextView object, which was showing the error message. So I tried to disable the error completely with setting

mTextInputLayout.setErrorEnabled(false);

and it works and looks fine, BUT I can't set it on again. When calling

mTextInputLayout.setErrorEnabled(true);
mTextInputLayout.setError("This field is required");

again I just see a read line, NOT the error message, so it seems the TextView which was showing the error message was destroyed and not created again.

I read here, that the TextView objects gets destroyed, when setErrorEnabled(false) is called and it seems it is not created again. Bug or feature?

The source for this control is not yet available in AOSP so I used the decompiler built in to Android Studio to examine the code to understand what was going wrong. I found that setErrorEnabled() actually creates and destroys a TextView object, whereas I was expecting it to simply toggle the visibility.

dabo248
  • 3,367
  • 4
  • 27
  • 37

3 Answers3

10

In case someone faces the same problem, I found a workaround that works fine. Just set the visibility of the error TextView object on and off, don't destroy the object.

Use this for enabling the error message:

if (textInputLayout.getChildCount() == 2)
    textInputLayout.getChildAt(1).setVisibility(View.VISIBLE);

textInputLayout.setError("This field is required");

and this for disabling the error message:

textInputLayout.setError(null);

if (textInputLayout.getChildCount() == 2)
    textInputLayout.getChildAt(1).setVisibility(View.GONE);
dabo248
  • 3,367
  • 4
  • 27
  • 37
  • 1
    Thanks, i was facing same issue. Don't know why Google implemented like this, but this is horrible that setErrorEnabled(true) is not working like intended -> error is not visible after setErrorEnabled(true) if we called setErrorEnabled(false) before – Ragaisis Oct 29 '15 at 08:55
  • I am facing java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/internal/widget/TintManager; at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:379) at textInputLayout.setError(null); – Nikhilesh Patve Mar 17 '16 at 13:34
  • You're importing the android support library into your project (in your build.gradle file)? dependencies { compile 'com.android.support:design:23.1.0' } – dabo248 Mar 17 '16 at 14:30
  • textInputLayout.getChildAt(1) may return a wrong view, if you also use a counter. – Sergei Vasilenko May 06 '16 at 09:53
0

As of Support library version 23.1.1 (and perhaps earlier), calling setErrorEnabled(false) will remove the error TextView and cause the TextInputLayout to display a new error when setError(String) is subsequently called.

However, there is still an existing bug where additional padding is not removed from the Layout once the error message is cleared. This bug can be worked around by using @dabo's post above:

https://code.google.com/p/android/issues/detail?id=200137

TheIT
  • 11,919
  • 4
  • 64
  • 56
0

In my case setting error, clearing error and setting error again caused a bug. A line hasn't become red again (API 23.4.0). This solution helped: TextInputLayout.setError() leaves empty space after clearing the error

Call setErrorEnabled(false) after setError(null).

Community
  • 1
  • 1
CoolMind
  • 26,736
  • 15
  • 188
  • 224