0

I have a TextInputLayout like this

<android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/edt_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/title_what_should_we_call_you" />

</android.support.design.widget.TextInputLayout>

For valid name when tap on Submit button I use

private boolean validateName() {
        if (edtName.getText().toString().trim().isEmpty()) {
            inputLayoutName.setErrorEnabled(true);
            inputLayoutName.setError("Enter name");    
            return false;
        } else {
            inputLayoutName.setErrorEnabled(false);
        }
        return true;
 }

When I run app, don't enter value to name and tap Submit, the error show (correct).
Then I enter some value to name -> tap Submit -> the error hide (correct)
But then I clear all name -> tap Submit -> the error dont shown

Why it happend? Any help or suggestion would be great appreciated.

If I don't use setErrorEnabled(false); and just set error message to null, the view of error message still invisible not gone

Linh
  • 57,942
  • 23
  • 262
  • 279
  • Is error enabling/disabling really needed ? – Shaishav Jul 25 '16 at 14:32
  • @Shaishav If I don't use setErrorEnabled(false); and just set error message to null, the view of error message still visible not gone – Linh Jul 25 '16 at 14:36
  • Uh...I only use the error showing functionality only when something is wrong with the input. If everything is fine, I simply don't call `setError()`. Why do you need to call `setError(null)`? – Shaishav Jul 25 '16 at 14:40
  • if I dont call `setError(null)` when name textfield is **valid**. the error message still visible – Linh Jul 25 '16 at 14:42
  • Yeah, I remember this... they added this as default functionality in newer version of android (wasn't there in older version, not sure of exact version, someone correct me if I'm wrong)...I see what you are trying to do, let me check it out.. – Shaishav Jul 25 '16 at 14:46
  • What happens when you use both `setError(null)` and `setErrorEnabled(false)`? – Shaishav Jul 25 '16 at 14:52

1 Answers1

0

I made a sample to test this and all I had to do was:

if (edtName.getText().toString().trim().isEmpty()) {
      inputLayoutName.setError("Enter name");    
      return false;
} else {
      inputLayoutName.setErrorEnabled(false);
}

return true;

The errors reappears as per in your use case should. Can you check with this please?

Shaishav
  • 5,282
  • 2
  • 22
  • 41