You use a TextInputLayout
and you use setError()
to define the message, setErrorEnabled()
to enable it, for example:
//Validación de error en email, valida texto que ingresa el usuario en el EditText.
boolean emailError = validaEditTextEmail();
//Dependiendo del valor booleando al validar el email, muestra el mensaje:
TextInputLayout tilEmail = (TextInputLayout) rootView.findViewById(R.id.til_email);
tilEmail.setError(emailError ? "Ingrese un correo válido" : "");
tilEmail.setErrorEnabled(emailError);
When an invalid email is detected, the error message is enabled:
I add an example t of TextInputLayout for the previous example:
<android.support.design.widget.TextInputLayout
android:id="@+id/til_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<EditText
android:id="@+id/mi_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/preg_correo"
android:inputType="textEmailAddress"
android:textSize="16sp"/>
</android.support.design.widget.TextInputLayout>
In this example, validation is performed on the text entered in EdiText
with id mi_email
, based on the value obtained we determine whether or not to show the message by means of TextInputLayout
.