oxcened
oxcened

Reputation: 175

TextInputLayout set error without message?

Is is possible to set error on Text Input Layout without showing any error message (i'm already doing it in another place)?.

textinputlayout.setError("");

won't work unfortunately.

What i basically need is the textInputLayout to change its line color to red, but i need to do it programmatically. Thank you

Upvotes: 17

Views: 5945

Answers (3)

Max Diland
Max Diland

Reputation: 296

The error text (even " ") will take/reserve some height in the TextInputLayout. If you also want to get rid of it then

<com.google.android.material.textfield.TextInputLayout      
    ...
    app:errorTextAppearance="@style/MyZeroSizeTextAppearance"
    ...
    >

Text appearance:

<style name="MyZeroSizeTextAppearance">
    <item name="android:textSize">0sp</item>
</style>

and then

textinputlayout.setError(" ");

Upvotes: 9

Ainege
Ainege

Reputation: 227

You can hide the layout with error like this:

textinputlayout.setError("");
if (textinputlayout.getChildCount() == 2) {
    textinputlayout.getChildAt(1).setVisibility(View.GONE);
}

Upvotes: 11

Simon
Simon

Reputation: 2733

Hope it's not too late, but the code behind setError is:

if (!mErrorEnabled) {
   if (TextUtils.isEmpty(error)) {
      // If error isn't enabled, and the error is empty, just return
      return;
   }
}

this means a simple workaround would be:

textinputlayout.setError(" ");

please be advised, that this will make TextView with error message visible - so it will make TextInputLayout higher even if it will be empty

since this passes the not-so-well-thought-out case of handling an empty error message request.

Upvotes: 9

Related Questions