30

Had a crash while trying to use the new TextInputField for Android and wanted to share my solution.

Trying the new TextInputField in the android appcompat library was crashing my app. Here was my layout xml.

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="e-mail"
        android:inputType="textEmailAddress"
        android:singleLine="true"/>

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

The error I got:

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

SOLUTION: Add the hintTextAppearance attribute to your TextInputLayout, so the lead tag looks like this:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextAppearance="@android:style/TextAppearance.Medium">
1
  • Your solution worked, thanks!
    – temirbek
    Jan 18, 2017 at 13:33

13 Answers 13

43

Make sure you have the following dependencies in your gradle file:

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

Working example:

<android.support.design.widget.TextInputLayout
    android:id="@+id/txtEmail_InpLyt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/txtEmail"
        android:hint="Email Address"
        android:singleLine="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</android.support.design.widget.TextInputLayout>

(Setting hintTextAppearance is not necessary.)

Update:

If you experience issues with the hint text not appearing in newer versions of Android (Marshmallow / Nougat), update library to version 22.2.1 (see TextInputLayout not showing EditText hint before user focus on it).

compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
4
  • This works, but I was really surprised that to get the TextInputLayout to work properly you have to add another library. Seems poorly done.
    – eimmer
    Nov 21, 2016 at 15:48
  • compile 'com.android.support:appcompat-v7:25.0.1' compile 'com.android.support:design:25.0.1' } Jan 19, 2017 at 1:02
  • @GilSnovsky i have updated my libraries but some of my app users (android api level 23 )are still getting this frustrating error Caused by android.view.InflateException: Binary XML file line #17: Binary XML file line #2: Error inflating class android.support.design.widget.TextInputLayout Mar 13, 2017 at 6:58
  • 1
    It works for me, however you may need to: Build -> Clean Project, then rebuild/run again.
    – Ibrahim.H
    Nov 1, 2017 at 18:26
24

This happened to me as well, and I came up with a solution that does not require changing the App Theme, but merely changing the Theme of the TextInputLayout:

<android.support.design.widget.TextInputLayout
    android:id="@+id/testingInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.AppCompat">

   <EditText
       android:id="@+id/testingEditText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:hint="@string/testText"
       android:inputType="textEmailAddress" />

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

You will need to add the appCompat library if you have not already:

compile 'com.android.support:appcompat-v7:23.0.1'
19

in adroidx it worked for me i have the libraries

 implementation 'com.android.support:appcompat-v7:28.0.0'
    //design widget
    implementation 'com.android.support:design:28.0.0'

i change

<android.support.design.widget.TextInputLayout

for

<com.google.android.material.textfield.TextInputLayout
2
  • Perfect. Guys this is the right answer at this time of the year b.c the reason it keeps crashing is b.c support library changed from android to androidx
    – Yosidroid
    Oct 6, 2019 at 20:05
  • Also you can change support design dependency to :implementation 'com.google.android.material:material:1.3.0-alpha02' now suported on androidx Aug 13, 2020 at 11:00
4

I got the same issue when inflating XML containing TextInputLayout. The problem was fixed by setting the correct Style on my application. Just like it says here : android design support library

I've the following issue

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

My style.xml was

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material">
        <!-- Customize your theme here. -->
    </style>
</resources>

As it said in this post on Design Support Library

Note that as the Design library depends on the Support v4 and AppCompat Support Libraries, those will be included automatically when you add the Design library dependency.

So NO NEED TO ADD the following line inside the gradle file

compile 'com.android.support:appcompat-v7:22.2.0'

I found the link behove explaining that the Design Support Library is part of the AppCompat and it require the AppCompat Theme base to work. So I've modify my style.xml to be

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
</resources>

And it worked.

2
  • It's not clear which part of the question you linked is "setting the correct Style". Can you show what you mean with a quick sample, like a line of code/config? (It's best practice to summarize a link anyways, just in case its target changes :)
    – kdbanman
    Jul 14, 2015 at 19:51
  • -1 It might work but what is the use of using support library if you add parent="android:Theme.Material" you have to change the api to 21. @pierre Nov 10, 2015 at 11:20
2

just to be clear as of Android Studio 3.* this is now changed to

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'

(if this goes red a squiggly when you add it accept the version Android Studio wants to suggest)

this needs to be inside the

 dependencies {}

section of build.gradle (Module:app) so it looks like this

dependencies {
    ...
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
}

then hit sync now

1

Use the following:

View view = LayoutInflator.from(getActivity()).inflate(R.layout.your_layout_file,parent,false);
1

Make sure the LayoutInflater is created from an AppCompatActivity

ex.

instead of

inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

use:

inflater = LayoutInflater.from(<appCompatActivity>);
1

None of the solution worked for me. I am using

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

Tried all the soluton. My app runs perfectly in devices with lower sdk. But crashes in newer( greater than sdk 26) devices. But after scratching head for 2 days finally got the solution.

my layout editor showed the error of

The following classes could not be instantiated: - android.support.design.widget.TextInputLayout

enter image description here But it did not gave me enough information to work with. It was not until I looked at other errors at the layout editor. Under render problem I found that

Couldn't resolve resource @string/path_password_strike_through

enter image description here

I finally got the solution after investigating this render problem. All you need to do is add

app:passwordToggleDrawable="@string/string_name"

in your TextInputLayout xml file.

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/login_input_layout_password"
                    android:layout_width="match_parent"
                    app:passwordToggleDrawable="@drawable/ic_lock_outline_grey600_24dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="10dp"
                    android:backgroundTint="@color/colorAccent"
                    app:boxStrokeColor="#555"
                    android:textColorHint="@color/colorPrimary">

                    <EditText
                        android:id="@+id/login_input_password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />

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

Finally I got this working...as per as the latest changes in the library itself, change the library from

<android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt_input"
        android:layout_margin="@dimen/activity_horizontal_margin"
        app:layout_constraintTop_toBottomOf="@+id/edt_first_name"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        app:hintEnabled="false">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Floating Hint Disabled" />

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

to

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt_input"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        app:layout_constraintTop_toBottomOf="@+id/edt_first_name"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        app:hintEnabled="false">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Floating Hint Disabled" />

    </com.google.android.material.textfield.TextInputLayout>
0

you might have added it compile 'com.android.support:appcompat-v7:22.2.0' but you need to add compile 'com.android.support:design:22.2.0'

you can try this dependencies:

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

in your gradle file.

0

I had the same issue with Android Studio 3.0.1

All you need to add the following line into gradle.properties (Project Propeties):

android.enableAapt2=false

1
  • #UPDATE : Change the layout theme, where TextInputField is defined. Example: (in my case, I used TextInputField in activity_main.xml so it will be like: <?xml version="1.0" encoding="utf-8"?> <LinearLayout .... ::::: android:theme="@style/AppTheme"> --> AppTheme should be a child of Theme.AppCompat or Decendent Jul 27, 2018 at 6:49
0

for me work changes from EditText to TextInputEditText

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/date_raffle"/>

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

Using implementation 'com.google.android.material:material:1.1.0' all you have to do is use your XML tags like this: <com.google.android.material.textfield.TextInputLayout for it to work...In my case it applies to my <com.google.android.material.textfield.TextInputEditText as well. Happy coding!...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.