0

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"
            >
Kingofkech
  • 129
  • 9
  • Hello, see [similar post] (https://stackoverflow.com/questions/30578976/android-textinputfield-inflator-error) [One more here](https://stackoverflow.com/questions/30958972/inflateexception-with-textinputlayout-and-alertdialog) [Or sample] (https://stackoverflow.com/questions/30953449/design-android-edittext-to-show-error-message-as-described-by-google) – Maxime Jallu Nov 28 '17 at 10:09
  • How are you getting your loginInputLayout? – jorjSB Nov 28 '17 at 10:11
  • this is the problem , i have all the dependencies and used the exact same instructions and it does not work , i've read them all – Kingofkech Nov 28 '17 at 10:11
  • see my edits, i've updated my post – Kingofkech Nov 28 '17 at 10:15

2 Answers2

2

Try this

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name" />

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

and

TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout);
til.setError("Text");
Anil
  • 1,605
  • 1
  • 14
  • 24
  • already seen this answer in an other post and it does not work , 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` – Kingofkech Nov 28 '17 at 10:20
  • +1, Since API 26 it's better to put hint and inputType to TextInputLayout for Autofill Framework, in other case you can get NPE because of this issue https://issuetracker.google.com/issues/67675432 – Viktor Yakunin Nov 28 '17 at 10:25
  • @Kingofkech app:errorEnabled is optional, if you go to source you can find that whenever you call setError("error") method it sets errorEnabled to true – Viktor Yakunin Nov 28 '17 at 10:27
  • just change the theme and check android:theme="@style/Theme.AppCompat" – Anil Nov 28 '17 at 10:28
  • or try this ans https://stackoverflow.com/questions/30958972/inflateexception-with-textinputlayout-and-alertdialog – Anil Nov 28 '17 at 10:30
1

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

Mohammed Farhan
  • 1,120
  • 8
  • 14