TextInputLayout error after entering value in edittext

How can I hide a TextInputLayout error after entering one text in an EditText . Is it possible?

How can I achieve this, or am I doing something wrong here. !!

enter image description here

the code

  layoutEdtPhone =(TextInputLayout)rootView.findViewById(R.id.layoutEdtPhone); layoutEdtPhone.setErrorEnabled(true); layoutEdtPhone.setError(getString(R.string.ui_no_phone_toast)); layoutEdtPassword = (TextInputLayout)rootView.findViewById(R.id.layoutEdtPassword); layoutEdtPassword.setErrorEnabled(true); layoutEdtPassword.setError(getString(R.string.ui_no_password_toast)); edtPhone=(EditText)rootView.findViewById(R.id.edtPhone); edtPassword=(EditText)rootView.findViewById(R.id.edtPassword); 

XML

  <EditText android:id="@+id/edtPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:background="@drawable/edt_background_selector" android:drawableLeft="@drawable/phone_icon" android:drawableStart="@drawable/phone_icon" android:hint="@string/phone" android:inputType="phone" android:padding="5dip" android:singleLine="true" android:textSize="14sp" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/layoutEdtPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/edtPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:background="@drawable/edt_background_selector" android:drawableLeft="@drawable/password_icon" android:drawableStart="@drawable/password_icon" android:hint="@string/password" android:inputType="textPassword" android:padding="5dip" android:singleLine="true" android:textSize="14sp" /> </android.support.design.widget.TextInputLayout> 
+8
android material-design android-textinputlayout
source share
8 answers

To further illustrate the answer given by Pretend, TextInputLayout does not perform the check itself. This is just a mechanism showing an error or a hint. You are responsible for installing / clearing the error. Here is how you can do it. Please note that in addition to TextChangedListener, you may also need OnFocusChangeListener to set an error when the user moves to the second text editing without any changes in the first field.

 protected void onCreate(Bundle savedInstanceState) { //..... edtPhone.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) { } @Override public void afterTextChanged(Editable s) { validateEditText(s); } }); edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { validateEditText(((EditText) v).getText()); } } }); } private void validateEditText(Editable s) { if (!TextUtils.isEmpty(s)) { layoutEdtPhone.setError(null); } else{ layoutEdtPhone.setError(getString(R.string.ui_no_password_toast)); } } } 
+11
source share

You can set layoutEdtPhone.setErrorEnabled(false);

+8
source share

Do

 mTextInputLayout.setError(null); 

to delete the error message.

A good practice might be to check for such errors:

 @Override public void checkErrorBeforeAction() { boolean error = false; mTextInputLayout.setError(null); if (mEditText.getText().toString().length == 0)) { mTextInputLayout.setError("Field empty"); } else if (mEditText.getText().toString().isValid) { // Other condition mTextInputLayout.setError("Field is invalid"); } if (!error) { // Call action } } 

In this way, it updates the error message before installing a new one.

+5
source share

I used @TextChanged ButterKnife and worked for me, look:

 @Bind(R.id.layoutEdtPhone) TextInputLayout tlayoutEdtPhone; @Bind(R.id.edtPhone) EditText edtPhone; //start ButterKnife (I spent the URL with full description for initilize) @OnTextChanged(R.id.edtPhone) public void changedTextOnEditPhone() { tlayoutEdtPhone.setError(""); } 

If you want to know about ButterKnife, I wrote a message in more detail, but it was done in my native language, i.e. pt_br. http://blog.alura.com.br/aumentando-a-produtividade-com-butter-knife-no-android/

+4
source share
 textInputLatout.getEditText().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) { if (s.length() < 1) { textInputLayout.setErrorEnabled(true); textInputLayout.setError("Please enter a value"); } if (s.length() > 0) { textInputLayout.setError(null); textInputLayout.setErrorEnabled(false); } } @Override public void afterTextChanged(Editable s) { } }); 
+2
source share
0
source share

Use the if else condition if the condition is true. TextInoutLayoutObj.setError (null); if his lie fixed your mistake there

0
source share

If you want to remove the error display from TextInputLayout, use: -

 YourtextInputLayout.setErrorEnabled(false); 

If you want to remove the error display from edittextfield, use: -

 edittext_id.setError(null); 

I suggest using Textwatcher fucntion in Android for more efficient validation.

eg:

 editText.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) { } @Override public void afterTextChanged(Editable s) { if (!validatefName()) { return; } } }); 
0
source share

All Articles