16

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

oxcened
  • 165
  • 1
  • 8
  • Check [http://stackoverflow.com/questions/40838069/programmatically-changing-underline-color-of-edittext] – RoHiT Mar 13 '17 at 10:03
  • thank you but i did already try that, it is meant to work only for edit text without text input layout – oxcened Mar 13 '17 at 10:57

3 Answers3

12

You can hide the layout with error like this:

textinputlayout.setError("");
if (textinputlayout.getChildCount() == 2) {
    textinputlayout.getChildAt(1).setVisibility(View.GONE);
}
Ainege
  • 219
  • 3
  • 8
9

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.

Filipkowicz
  • 639
  • 6
  • 17
Simon
  • 2,643
  • 3
  • 40
  • 61
  • While I don't think that this case isn't well-thought-out, it might simply mean that the TextInputLayout error was meant to take up space and to do that it must be something non-null or non-empty - this answer should get more upvotes since the solution is more manageable. – devanshu_kaushik May 21 '20 at 06:08
  • textinputlayout.setError(" "); this is the fastest solution. Thanks. – Khang Tran Mar 11 '21 at 03:11
5

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(" ");
Max Diland
  • 246
  • 3
  • 5