EditText with error message on Android

2

In the Google Material Design Design Guide , to show an error message when a field is incorrectly filled in, an error message is used below the component, I wonder how that text is established and what method it launches for its appearance, how to also subsequently hide it if the user re-entered the data They are correct.

I leave the image that may be clearer than I want to say

    
asked by Webserveis 18.04.2016 в 18:00
source

1 answer

2

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 .

    
answered by 18.04.2016 / 18:12
source