web-dev-qa-db-fra.com

Message d'erreur du message d'erreur Android EditText

J'ai un problème dans mon application où le message d'erreur sur EditTexts s'affiche mais le texte n'est pas visible . Il se présente comme suit:

 EditText error message

Cela se produit avec toutes les EditTexts de mon application . Voici un exemple de mise en page XML

Disposition: 

<?xml version="1.0" encoding="utf-8"?>

<Android.support.design.widget.CoordinatorLayout
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:id="@+id/main_content"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="@drawable/gradient"
    Android:fitsSystemWindows="true"
    tools:context=".ui.onboarding.profile.OnboardingUserProfileActivity">
    <ImageView
        Android:id="@+id/imageView"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:scaleType="fitXY"
        app:srcCompat="@drawable/shapes_background" />

    <ProgressBar
        Android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_marginBottom="8dp"
        Android:visibility="gone" />

    <ScrollView
        Android:id="@+id/form"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_marginLeft="20dp"
        Android:layout_marginRight="20dp"
        Android:layout_marginBottom="20dp">

        <LinearLayout
            Android:id="@+id/linearLayout"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_marginTop="10dp"
            Android:layout_marginBottom="80dp"
            Android:orientation="vertical">

            <Android.support.constraint.ConstraintLayout
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"
                Android:layout_gravity="center"
                Android:layout_marginTop="40dp"
                Android:layout_marginBottom="20dp">

                <ImageView
                    Android:id="@+id/profileImageView"
                    Android:layout_width="160dp"
                    Android:layout_height="160dp"
                    Android:scaleType="fitXY"
                    Android:src="@drawable/user_profile_placeholder"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>


                <Android.support.design.widget.FloatingActionButton
                    Android:id="@+id/cameraImageButton"
                    Android:layout_width="wrap_content"
                    Android:layout_height="wrap_content"
                    Android:layout_gravity="bottom|end"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:srcCompat="@Android:drawable/ic_menu_camera" />
            </Android.support.constraint.ConstraintLayout>

            <Android.support.design.widget.TextInputLayout
                Android:id="@+id/firstnameTvLayout"
                Android:layout_width="match_parent"
                Android:layout_height="0dp"
                Android:layout_marginBottom="20dp"
                Android:layout_weight="0.33"
                Android:textColorHint="@color/colorVeryLightGray"
                Android:theme="@style/AppTheme.WhiteColorAccent"
                app:errorTextAppearance="@style/error_appearance">

                <Android.support.design.widget.TextInputEditText
                    Android:id="@+id/firstnameTv"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:hint="@string/Prompt_firstname"
                    Android:inputType="textPersonName"
                    Android:maxLines="1"
                    Android:nextFocusDown="@id/lastnameTv"
                    Android:nextFocusForward="@id/lastnameTv"
                    Android:singleLine="true"
                    Android:textColor="@Android:color/white"
                    Android:textColorHint="@color/colorWhite"
                    Android:theme="@style/AppTheme.WhiteEditText"/>

            </Android.support.design.widget.TextInputLayout>

            <Android.support.design.widget.TextInputLayout
                Android:id="@+id/lastnameTvLayout"
                Android:layout_width="match_parent"
                Android:layout_height="0dp"
                Android:layout_marginBottom="20dp"
                Android:layout_weight="0.33"
                Android:textColorHint="@color/colorVeryLightGray"
                Android:theme="@style/AppTheme.WhiteColorAccent">

                <Android.support.design.widget.TextInputEditText
                    Android:id="@+id/lastnameTv"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:hint="@string/Prompt_lastname"
                    Android:inputType="textPersonName"
                    Android:maxLines="1"
                    Android:nextFocusDown="@id/usernameTv"
                    Android:nextFocusForward="@id/usernameTv"
                    Android:singleLine="true"
                    Android:textColor="@Android:color/white"
                    Android:textColorHint="@color/colorWhite"
                    Android:theme="@style/AppTheme.WhiteEditText" />

            </Android.support.design.widget.TextInputLayout>

            <Android.support.design.widget.TextInputLayout
                Android:id="@+id/usernameTvLayout"
                Android:layout_width="match_parent"
                Android:layout_height="0dp"
                Android:layout_weight="0.33"
                Android:textColorHint="@color/colorVeryLightGray"
                Android:theme="@style/AppTheme.WhiteColorAccent">

                <Android.support.design.widget.TextInputEditText
                    Android:id="@+id/usernameTv"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:hint="@string/Prompt_username"
                    Android:imeActionId="6"
                    Android:imeActionLabel="@string/action_sign_in_short"
                    Android:imeOptions="actionUnspecified"
                    Android:maxLines="1"
                    Android:nextFocusDown="@id/nextBtn"
                    Android:nextFocusForward="@id/nextBtn"
                    Android:singleLine="true"
                    Android:textColor="@Android:color/white"
                    Android:textColorHint="@color/colorWhite"
                    Android:theme="@style/AppTheme.WhiteEditText" />

            </Android.support.design.widget.TextInputLayout>

        </LinearLayout>
    </ScrollView>

    <Button
        Android:id="@+id/nextBtn"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_marginBottom="10dp"
        Android:layout_marginLeft="50dp"
        Android:layout_marginRight="50dp"
        Android:layout_marginTop="16dp"
        Android:text="@string/next"
        Android:textStyle="bold"
        Android:theme="@style/AppTheme.PrimaryButton"
        app:layout_anchor="@+id/form"
        app:layout_anchorGravity="bottom|center_horizontal" />

</Android.support.design.widget.CoordinatorLayout>

Et les styles:

<style name="AppTheme.WhiteColorAccent">
    <item name="colorAccent">@color/colorWhite</item>
</style>
  <style name="AppTheme.WhiteEditText" parent="Widget.AppCompat.EditText">
    <item name="Android:textColor">@color/colorWhite</item>
    <item name="colorControlNormal">@color/colorVeryLightGray</item>
    <item name="colorControlActivated">@color/colorWhite</item>
    <item name="colorControlHighlight">@color/colorWhite</item>
</style>

Erreur de paramétrage dans l'activité avec firstnameTv.error = "This field can not be empty" (Kotlin)

6
Inn0vative1

Vous devez utiliser style="@style/AppTheme.WhiteEditText"

Au lieu de Android:theme="@style/AppTheme.WhiteColorAccent"

Maintenant, la question est pourquoi faut-il utiliser style au lieu de Android:theme

Lorsque vous utilisez style, cela s’applique seulement à view

et lorsque vous utilisez Android:theme, cette option s’appliquera à view ainsi qu’à l’ensemble de ses children.

En savoir plus sur What is the difference between style and Android:theme attributes?

SAMPLE CODE

<?xml version="1.0" encoding="utf-8"?>
<Android.support.design.widget.CoordinatorLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:id="@+id/main_content"
    Android:layout_width="match_parent"
    Android:fitsSystemWindows="true"
    Android:layout_height="match_parent"
    Android:background="@color/colorPrimary">

    <ImageView
        Android:id="@+id/imageView"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:scaleType="fitXY"
        Android:visibility="gone" />

    <ProgressBar
        Android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_marginBottom="8dp"
        Android:visibility="gone" />

    <ScrollView
        Android:id="@+id/form"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_marginLeft="20dp"
        Android:layout_marginRight="20dp"
        Android:layout_marginBottom="20dp"
        Android:fillViewport="true">

        <LinearLayout
            Android:id="@+id/linearLayout"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_marginTop="10dp"
            Android:layout_marginBottom="80dp"
            Android:orientation="vertical">

            <Android.support.constraint.ConstraintLayout
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"
                Android:layout_gravity="center"
                Android:layout_marginTop="40dp"
                Android:layout_marginBottom="20dp">

                <ImageView
                    Android:id="@+id/profileImageView"
                    Android:layout_width="160dp"
                    Android:layout_height="160dp"
                    Android:scaleType="fitXY"
                    Android:src="@color/colorNavBar"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


                <Android.support.design.widget.FloatingActionButton
                    Android:id="@+id/cameraImageButton"
                    Android:layout_width="wrap_content"
                    Android:layout_height="wrap_content"
                    Android:layout_gravity="bottom|end"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:srcCompat="@Android:drawable/ic_menu_camera" />
            </Android.support.constraint.ConstraintLayout>

            <Android.support.design.widget.TextInputLayout
                Android:id="@+id/firstnameTvLayout"
                Android:layout_width="match_parent"
                Android:layout_height="0dp"
                Android:layout_marginBottom="20dp"
                Android:layout_weight="0.33"
                Android:textColorHint="@color/colorVeryLightGray"
                style="@style/AppTheme.WhiteColorAccent"
                >

                <Android.support.design.widget.TextInputEditText
                    Android:id="@+id/firstnameTv"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:hint="First Name"
                    Android:inputType="textPersonName"
                    Android:maxLines="1"
                    Android:imeOptions="actionNext"
                    Android:nextFocusDown="@id/lastnameTv"
                    Android:nextFocusForward="@id/lastnameTv"
                    Android:textColor="@Android:color/white"
                    Android:textColorHint="@color/colorWhite"
                    style="@style/AppTheme.WhiteEditText" />

            </Android.support.design.widget.TextInputLayout>

            <Android.support.design.widget.TextInputLayout
                Android:id="@+id/lastnameTvLayout"
                Android:layout_width="match_parent"
                Android:layout_height="0dp"
                Android:layout_marginBottom="20dp"
                Android:layout_weight="0.33"
                Android:textColorHint="@color/colorVeryLightGray"
                style="@style/AppTheme.WhiteColorAccent">

                <Android.support.design.widget.TextInputEditText
                    Android:id="@+id/lastnameTv"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:hint="Last Name"
                    Android:inputType="textPersonName"
                    Android:maxLines="1"
                    Android:nextFocusDown="@id/usernameTv"
                    Android:nextFocusForward="@id/usernameTv"
                    Android:singleLine="true"
                    Android:textColor="@Android:color/white"
                    Android:textColorHint="@color/colorWhite"
                    style="@style/AppTheme.WhiteEditText" />

            </Android.support.design.widget.TextInputLayout>

            <Android.support.design.widget.TextInputLayout
                Android:id="@+id/usernameTvLayout"
                Android:layout_width="match_parent"
                Android:layout_height="0dp"
                Android:layout_marginBottom="20dp"
                Android:layout_weight="0.33"
                Android:textColorHint="@color/colorVeryLightGray"
                style="@style/AppTheme.WhiteColorAccent">

                <Android.support.design.widget.TextInputEditText
                    Android:id="@+id/usernameTv"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:hint="user Name"
                    Android:imeActionId="6"
                    Android:imeActionLabel="AB"
                    Android:imeOptions="actionUnspecified"
                    Android:maxLines="1"
                    Android:nextFocusDown="@id/nextBtn"
                    Android:nextFocusForward="@id/nextBtn"
                    Android:singleLine="true"
                    Android:textColor="@Android:color/white"
                    Android:textColorHint="@color/colorWhite"
                    style="@style/AppTheme.WhiteEditText" />

            </Android.support.design.widget.TextInputLayout>

        </LinearLayout>
    </ScrollView>

    <Button
        Android:id="@+id/nextBtn"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_marginBottom="10dp"
        Android:layout_marginLeft="50dp"
        Android:layout_marginRight="50dp"
        Android:layout_marginTop="16dp"
        Android:text="Next"
        Android:textStyle="bold"
        app:layout_anchor="@+id/form"
        app:layout_anchorGravity="bottom|center_horizontal" />
</Android.support.design.widget.CoordinatorLayout>

style

<style name="AppTheme.WhiteColorAccent">
    <item name="colorAccent">@color/colorWhite</item>
</style>

<style name="AppTheme.WhiteEditText" parent="Widget.AppCompat.EditText">
    <item name="Android:textColor">@color/colorWhite</item>
    <item name="colorControlNormal">@color/colorVeryLightGray</item>
    <item name="colorControlActivated">@color/colorWhite</item>
    <item name="colorControlHighlight">@color/colorWhite</item>
</style>

SORTIE

 enter image description here

7
Nilesh Rathod

Vous devez définir l'erreur dans TextInputLayout, PAS dans TextInputEditText

firstnameTvLayout.setError("Error goes here");

Assurez-vous également que vous avez terminé

firstnameTvLayout.setErrorEnabled(true);

Voir la documentation ici pour plus de détails https://developer.Android.com/reference/Android/support/design/widget/TextInputLayout

1
Irshad P I

Vous devez utiliser style au lieu de Android:theme. Si vous définissez Android:theme sur un groupe de vues, ses enfants (un message d'erreur dans ce cas) utiliseront le même style.


Votre code

<Android.support.design.widget.TextInputLayout
    ....
    Android:theme="@style/AppTheme.WhiteColorAccent"
    ....>

Le changer en

<Android.support.design.widget.TextInputLayout
    ....
    style="@style/AppTheme.WhiteColorAccent"
    ....>
0
Anees
textInputLayout.setErrorEnable(true);
0
Rajan Aghara