TextInputLayout.setError() leaves empty space after clearing the error TextInputLayout.setError() leaves empty space after clearing the error android android

TextInputLayout.setError() leaves empty space after clearing the error


Check out the docs for

public void setErrorEnabled (boolean enabled)

It says

Whether the error functionality is enabled or not in this layout. Enabling this functionality before setting an error message via setError(CharSequence), will mean that this layout will not change size when an error is displayed.

Well based on this, try setting setErrorEnabled(true) before setError(), and, set setErrorEnabled(false) after setError(null).


Method setErrorEnabled(false) will clear the extra space, so call it after setError(null).


Dont use setErrorEnabled(boolean), it just doesnt show up the error from the second time.

public class MyTextInputLayout extends android.support.design.widget.TextInputLayout {public MyTextInputLayout(Context context) {    super(context);}public MyTextInputLayout(Context context, AttributeSet attrs) {    super(context, attrs);}public MyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);}@Overridepublic void setError(@Nullable CharSequence error) {    super.setError(error);    View layout = getChildAt(1);    if (layout != null) {        if (error != null && !"".equals(error.toString().trim())) {            layout.setVisibility(VISIBLE);        } else {            layout.setVisibility(GONE);        }    }}}

Then just setError(errorMessage); or setError(null);