I have a simple validation in my app, here I am using four EdtiText
.I am showing error when EditTex
t loses focus but, the problem is on losing focus EditText
only shows icon without error message. I have tried using requestFocus()
method and can now see the error but the problem is.. now my form shows two cursors and even if the first field is not valid and showing errors, whatever I am typing goes into second EdtiText
. Can anybody help me to fix this?
Thanks.
Here is my xml file -
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="first name"
android:id="@+id/edt_first_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="last name"
android:id="@+id/edt_last_name"
android:singleLine="true"
android:layout_below="@+id/edt_first_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"
android:singleLine="true"
android:id="@+id/edt_email"
android:layout_below="@+id/edt_last_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:singleLine="true"
android:imeOptions="actionNext"
android:id="@+id/edt_password"
android:layout_below="@+id/edt_email"/>
and here is my main file where i am cheking validations.
firstname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (!Validate(firstname.getText().toString())) {
} else {
firstname.setFocusable(true);
firstname.setError("not valid");
}
}else{
firstname.setError(null);
}
}
});
Use
TextInputLayout
for materialEditText
like :and in your
Activity