Search code examples
androidandroid-textinputlayout

loginInputText.setError("erreur"); gives me an error


I am trying to show an error under my edit text, like the one used in the Material design:

 <android.support.design.widget.TextInputLayout
            android:id="@+id/login_login_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:theme="@style/TextLabel">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/login_login_input_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/login_edit_text_background"
                android:drawableEnd="@drawable/icone_info"
                android:drawablePadding="2dp"
                android:hint="@string/login"
                android:imeOptions="actionNext"
                android:maxLines="1"
                android:textColor="@color/colorGray"
                android:textSize="12sp" />

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

i've tried using loginInputText.setError("error");

and

loginInputLayout.setError("error");

And it does not seem to work, i've seen in stack that i need to implement:

loginInputLayout.setErrorEnabled(true);

But whenever i try to use it i get this error (it's the same error when i try to use the setError alone :

  FATAL EXCEPTION: main
              Process: 475, PID: 9466
              android.view.InflateException: Binary XML file line #17: Error inflating class TextView

EDIT:

i get my logininputlayout and my logininputtext by :

 loginInputText = (TextInputEditText) findViewById(R.id.login_login_input_text);

EDIT2:

all the dependencies are there :

compile 'com.android.support:appcompat-v7:27.0.0'
compile 'com.android.support:design:27.0.0'

EDIT 3: Once i add the app:errorEnabled i get this error :

 android.view.InflateException: Binary XML file line #83: Error inflating class android.support.design.widget.TextInputLayout

EDIT 4:

My app theme already uses android:theme="@style/Theme.AppCompat" and my activity also inherits AppCompatActivity

EDIT 5:

if i try to use a normal EditTexti get this error :android.view.InflateException: Binary XML file line #17: Error inflating class TextView

EDIT 6:

This is the theme of my layout:

<style name="TextLabel" parent="TextAppearance.AppCompat">

    <item name="android:textColorHint">@color/colorLoginGray</item>
    <item name="android:textSize">16sp</item>
    <item name="colorAccent">@color/colorLoginBlack</item>
    <item name="colorControlNormal">@color/colorTextInputGray</item>
    <item name="colorControlActivated">@color/colorLoginBlack</item>
</style>

Edit 7: Solution

add a second theme and extend the main theme from AppCompat:

     <style name="TextLabel" parent="Theme.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/colorLoginGray</item>
    <item name="android:textSize">16sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/colorLoginBlack</item>
    <item name="colorControlNormal">@color/colorTextInputGray</item>
    <item name="colorControlActivated">@color/colorLoginBlack</item>
</style>

<style name="TextLabelError" parent="Theme.AppCompat">
    <item name="android:textColor">@color/errorRed</item>

</style>

and in the xml :

        <android.support.design.widget.TextInputLayout
            android:id="@+id/login_login_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:theme="@style/TextLabel"
            app:errorTextAppearance="@style/TextLabelError"
            >

Solution

  • TextInputLayout

     <android.support.design.widget.TextInputLayout
                android:id="@+id/userPasswordTIL"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginTop="5dp"
                android:clipToPadding="false"
                android:gravity="bottom"
                android:paddingTop="4dp"
                android:theme="@style/Theme.AppCompat"
                android:textColorHint="@color/white"
                >
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="bottom"
                    android:hint="Password"
                    android:inputType="textPassword"
                    android:maxLength="10"
                    android:paddingLeft="17dp"
                    android:paddingRight="17dp"
                    android:paddingTop="8dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    />
            </android.support.design.widget.TextInputLayout>
    

    and then

    userPasswordTIL = (TextInputLayout) findViewById(R.id.userPasswordTIL);
    userPasswordTIL.setError("Required"); #1 pic result
    
                 OR
    
    userPasswordTIL.getEditText().setError("Required"); #2 pic result
    

    #1 result#2 result