I have a form with multiple EditText
items each wrapped in a TextInputLayout
. When clicking the Submit button, I check to see if the EditText
is empty. If it is, I call an error on that field, e.g. ageWrapper.setError("Age required")
. This shows an error message under the field, and also changes the hint
of the EditText
to red. This occurs for all input fields on the page and the errors are working fine.
The problem occurs after I clear the error. I first leave the field blank and submit - error shows up and things are displayed red. I then type something into the field, and submit again. Now, the error goes away as it should (ageWrapper.setErrorEnabled(false)
), but the hint
remains in red rather than changing back to its normal, non-error, colour.
This should not be happening because the error has been cleared by typing something into the field, and yet the hint is still red implying an error, even though the error message itself (under the field) disappears.
When I click the field again, or any other field on the page, the red hint text then changes back to its normal colour.
Clearly the error display is working - it displays and goes away as it should. But why does the hint text remain red after Ive cleared the error by typing something into the field, and only changes back to its regular colour after a click to the field itself (or another field)? How do I get rid of this behaviour?
In my mind, if both fields are empty, both will display red and show error messages. If I then fill in one of the fields, setErrorEnabled(false)
should be set on that field and the hint text should go back to normal but it doesnt.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/new_layout_padding">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_condition_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/input_subnum_wrapper">
<EditText
android:id="@+id/input_condition"
android:layout_width="match_parent"
android:layout_height="@dimen/new_form_element_height"
android:hint="@string/input_text_condition"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_age_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/input_condition_wrapper">
<EditText
android:id="@+id/input_age"
android:layout_width="match_parent"
android:layout_height="@dimen/new_form_element_height"
android:hint="@string/input_text_age"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/input_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:ems="5"
android:text="@string/input_button_save" />
</RelativeLayout>
Java class for the fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new, container, false);
//Get all text fields
conditionWrapper = (TextInputLayout) view.findViewById(R.id.input_condition_wrapper);
ageWrapper = (TextInputLayout) view.findViewById(R.id.input_age_wrapper);
//Listener for create button
createButton = (Button) view.findViewById(R.id.input_submit);
createButton.setOnClickListener(this);
// Inflate the layout for this fragment
return view;
}
//Create/submit button click
@Override
public void onClick(View v) {
//Get input values
String condition = conditionWrapper.getEditText().getText().toString();
String age = ageWrapper.getEditText().getText().toString();
//If all the validation passes, submit the form. Else, show errors
if (!isEmpty(condition) & !isEmpty(age)) {
//Submit form data
} else {
if (isEmpty(condition)) {
conditionWrapper.setError("Condition required");
} else {
conditionWrapper.setErrorEnabled(false);
}
if (isEmpty(age)) {
ageWrapper.setError("Age required");
} else {
ageWrapper.setErrorEnabled(false);
}
}
}
//Check if a string is empty
public boolean isEmpty(String string) {
if (string.equals("")) {
return true;
} else {
return false;
}
}
change your code to
if (isEmpty(condition)) {
conditionWrapper.setError("Condition required");
} else {
conditionWrapper.setError(null);
}