1

This code is in a layout file.

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

    <EditText
        android:inputType="number"
        android:id="@+id/mobileNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLength="10"
        android:hint="Mobile Number" />

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

This is Java code.

TextInputLayout textInputLayoutMobile = (TextInputLayout)findViewById(R.id.textInputLayoutMobile);
textInputLayoutMobile.setErrorEnabled(true);
textInputLayoutMobile.setError("This field is required");

Current behaviour: When we click on the field, it shows, "This field is required". Also when we start typing in the field, this message does not go away.

Desired behaviour: When we click for the first time, it should not show, "This field is required". When we move to another field after touching this field and without entering any data, it should show, "This field is required". Also when we start typing in the field, this message should go away.

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
  • you could refer to [this answer](https://stackoverflow.com/questions/31808157/textinputlayout-error-after-enter-value-into-edittext) – Amr Apr 27 '18 at 22:46

4 Answers4

0

I think you must use text change listener

edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
             //set error
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

and on his method to set your desire output.. hope to help you!

Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37
0

I think you need to take the reference of EditText and not the TextInputLayout. Try

 EditText editText = (EditText)findViewById(R.id.mobileNumber);
 editText.setError("This field is required");
Sarfaraz
  • 324
  • 1
  • 3
  • 13
0

I believe the error is that those two lines of code are used while retrieving the textInputLayoutMobile from your layout (probably in onCreate). So the view is set to always display this error message.

textInputLayoutMobile.setErrorEnabled(true);
textInputLayoutMobile.setError("This field is required");

I suppose those two lines should be moved from this point and set there where you should validate the textInputLayoutMobile input and deciding that the validation is wrong, e.g. in an onClick callback or listener. Additionally when your input is validated to true then set textInputLayoutMobile.setErrorEnabled(false);

See an example here: http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168

pleft
  • 7,567
  • 2
  • 21
  • 45
0

I think you need take id="@+id/mobileNumber", set the widget and than try something like that

your_widget.setOnClickListener(new OnClickListener() {

        public void onClick(final View v) {
            if (yourwidget.getText().toString().length() == 0) {
                yourwidget.setError("This field is required");}

else etc...

You could use this control on a "next" button or something like that (save button etc) insted of use that on the field directly (sorry for my english)