You can use a Textwatcher together with a Textinputlayout. In your XML would be:
<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 be:
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 Textwatcher to your Edittext, just do:
txtEmail.addTextChangedListener(new MyTextWatcher(inputLayoutEmail));
And the result will be similar to this: