You can use a TextWatcher in conjunction with an TextInputLayout . Your XML would look like this:
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayoutEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="email"
android:hint="Username" />
</android.support.design.widget.TextInputLayout>
Your TextWatcher class would look like:
private class MyTextWatcher implements TextWatcher {
private TextInputLayout inputLayout;
private MyTextWatcher(TextInputLayout inputLayout) {
this.inputLayout=inputLayout;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void afterTextChanged(Editable editable) {
//Fazer a validação
if (/* Condição para ser um texto inválido */) {
inputLayout.setError("Erro. Esse email é inválido.");
} else {
inputLayout.setErrorEnabled(false);
}
}
}
And to apply the TextWatcher to your EditText, just do:
txtEmail.addTextChangedListener(new MyTextWatcher(inputLayoutEmail));
And the result will look like this: