Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout setErrorEnabled doesn't create new TextView object

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.

like image 731
dabo248 Avatar asked Oct 20 '15 07:10

dabo248


1 Answers

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);
like image 154
dabo248 Avatar answered Oct 26 '22 05:10

dabo248