50

I'm trying to use TextInputEditText from Material Design (https://github.com/material-components/material-components-android/blob/master/docs/components/TextInputLayout.md) and I'm getting runtime exception.

This's part of run log:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.grigori.materialtextedit, PID: 12036
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grigori.materialtextedit/com.example.grigori.materialtextedit.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
                  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
                  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
                  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:6669)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
               Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
               Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
               Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.material.textfield.TextInputLayout" on path: DexPathList

I cut this log by DexPathList that contains many paths to apk files, like:

zip file "/data/app/com.example.grigori.materialtextedit-jamgTcrgG9neBKIMcqDo7Q==/base.apk"

My xml file:

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_text"/>

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

My build.gradle dependences:

    dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}     
grasdy
  • 693
  • 1
  • 6
  • 10
  • 1
    The same issue is for me, I have added implementation in build.gradle too. It works for almost every devices. For some vivo devices, it is crashing every time. Any idea why the issue is only for vivo devices? – Midhun Murali Aug 05 '19 at 05:26

31 Answers31

49

Faced this issue when implementing AndroidX in my existing project.

implementation 'com.google.android.material:material:1.0.0-beta01'

Layout XML

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/userNameWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/TextLabel">

Old Style

<style name="TextLabel" parent="TextAppearance.AppCompat">
        <item name="android:textColorHint">@color/hint_color</item>
        <item name="android:textSize">@dimen/text_20sp</item>
        <item name="colorControlNormal">@color/primaryGray</item>
        <item name="colorControlActivated">@color/colorPrimary</item>
    </style>

New Style

<style name="TextLabel" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
        <item name="android:textColorHint">@color/hint_color</item>
        <item name="android:textSize">@dimen/text_20sp</item>
        <item name="colorControlNormal">@color/primaryGray</item>
        <item name="colorControlActivated">@color/colorPrimary</item>
    </style>
Mihir Palkhiwala
  • 2,586
  • 3
  • 37
  • 47
34

Update your themes in @style folder by inheriting one of the followings:

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar

like this:

    <style name="AppTheme.NoActionBar"    parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimaryCustom</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDarkCustom</item>
    <item name="colorAccent">@color/colorAccentCustom</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
Md. Yamin Mollah
  • 1,609
  • 13
  • 26
  • My particular problem was that I was trying to use a style with parent of "Theme.AppCompat.Light.NoActionBar" and some of the widgets were Material ones. They don't seem to play well together. (I'm learning Android and this is how I managed to fix my crashing app). – Ikon Jun 02 '20 at 15:38
25

It could just be a theme issue. If your TextInputLayout's parent is MaterialComponents (as described below)

<style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
   ...
</style>

And if your Activity (or App's) theme derives from AppCompat, your app will crash because MaterialComponents and AppCompat themes are not compatible. For example, the AppTheme (for the activity or the App) CANNOT be as follows:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

You will have to set your AppTheme to MaterialComponents as well, as below:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
   <item name="colorPrimary">@color/colorPrimary</item>
   <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
   <item name="colorAccent">@color/colorAccent</item>
</style>

Hopefully, this might work.

Ram Iyer
  • 1,621
  • 1
  • 23
  • 25
16

Thank's for your answers. The problem was that I used com.google.android.material.textfield.TextInputLayout. If you use this control you should add in your dependencies:

implementation 'com.google.android.material:material:1.0.0-beta01'

In my case, I rewrite my xml on android.support.design.widget.TextInputLayout:

    <android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColorHint="@color/colorPrimary"
    app:errorEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:hint="Username" />

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

Also, I've added in my dependencies:

implementation 'com.android.support:design:27.1.1'

This fixed my problem.

grasdy
  • 693
  • 1
  • 6
  • 10
14

I also faced the same error in AndroidX. I have a solution that worked for me.

Instead of using Theme.AppCompat, use Theme.MaterialComponents style as AppTheme or for the activity.

style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Use Google Material Library.

implementation 'com.google.android.material:material:1.2.0'

Why does this problem happen?

I noticed that you are using custom style for you TextInputLayout : style="@style/TextInputLayout"

This should be @style/Widget.MaterialComponents.TextInputLayout.OutlinedBox or similar. So it required Theme.MaterialComponents as parent theme on Activity.

Shubham Gupta
  • 1,123
  • 11
  • 16
  • 1
    I was able to avoid the issue just using the MaterialComponents, instead of the AppCompat in the styles, avoiding to use the alpha chanel of the material library – Ariel Jul 31 '20 at 21:27
10

Actually it is not correct what most people claim regarding the AppTheme!

If you're using androidx and do NOT want to have an MaterialComponents AppTheme

<!--style name="AppTheme" parent="Theme.MaterialComponents..."-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

You can always do the following and add the MaterialComponents Theme only to the parent container:

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:theme="@style/Theme.MaterialComponents">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/performing_attr1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Label"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="number"/>
            </com.google.android.material.textfield.TextInputLayout>
Tobi
  • 858
  • 7
  • 15
8

Change your app theme to inherit from a Material Components theme

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
   <!-- ... -->
</style>
Saad Lembarki
  • 482
  • 5
  • 6
5

Add this dependency in app Gradle

implementation 'com.google.android.material:material:1.1.0-alpha09'

Add this style in style.xml for your TextInputLayout

<style name="TextLabel" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="android:textColorHint">@color/colorGray600</item>
    <item name="android:textSize">@dimen/text_20sp</item>
    <item name="colorControlNormal">@color/colorGray900</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
</style>

Add this in your TextInputLayout like this

android:theme="@style/TextLabel"

and make sure you have used proper theme as parent theme like this

Theme.MaterialComponents.Light

Now this will solve InflateException problem. but if you have vector drawables then follow this steps bellow

1. You need to opt in to AndroidX vector support in your app’s build.gradle:

android {
...
defaultConfig {
    ...
    vectorDrawables.useSupportLibrary = true
} }

2. If you want to set drawables declaratively (i.e. in your layouts) then appcompat offers a number of *Compat attributes that you should use instead of the standard platform ones:

ImageView, ImageButton:

Don’t: android:src Do: app:srcCompat

CheckBox, RadioButton:

Don’t: android:button Do: app:buttonCompat TextView (as of appcompat:1.1.0): Don’t: android:drawableStart android:drawableTop etc. Do: app:drawableStartCompat app:drawableTopCompat etc.

As these attributes are part of the appcompat library, be sure to use the app: namespace. Internally these AppCompat* views use AppCompatResources themselves to enable loading vectors.

3. If you want to use inside code then

val drawable = AppCompatResources.getDrawable(context, R.drawable.my_vector)

**4.**If you are using Data Binding then this can be accomplished using a custom binding adapter:

/* Copyright 2018 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
@BindingAdapter("indeterminateDrawableCompat")
fun bindIndeterminateProgress(progressBar: ProgressBar, @DrawableRes id: Int) {
  val drawable = AppCompatResources.getDrawable(progressBar.context, id)
  progressBar.indeterminateDrawable = drawable
}

Hope this will solve the issue.

Jahangir Kabir
  • 1,783
  • 13
  • 17
4

If you are running into this error during an instrumentation test then you may need to define your style with. val scenario = launchFragmentInContainer<LoginFragment>(themeResId = R.style.MyTheme)

4

The main reason of this fatal error is while styling the text input layout it is very much required to change the theme of app as well or not app then create another style for the particular screen. This can be done by

Go to values then open styles.xml

this is usually your app theme will look like

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

the main thing to change here is

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

change the appcompat to material components

Incase if you dont want to change the app theme

<style name="MaterialAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

I think this will work fine and help you to inflate the outlined box or any particular theme in input field.

3

you Should change

build.gradle file in

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'

    testImplementation 'junit:junit:4.12'

    implementation 'com.google.android.material:material:1.0.0-beta01'
}

xml file in

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:focusable="false"
    android:clickable="true">


   <LinearLayout
       android:id="@+id/linear"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:orientation="vertical">

   <com.google.android.material.textfield.TextInputLayout
       style="@style/TextInputLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="UserName"
       android:layout_margin="15dp">

      <com.google.android.material.textfield.TextInputEditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="text"
          android:maxLines="1" />
   </com.google.android.material.textfield.TextInputLayout>

   <com.google.android.material.textfield.TextInputLayout
       style="@style/TextInputLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="Email"
       android:layout_margin="15dp">

      <com.google.android.material.textfield.TextInputEditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="textEmailAddress"
          android:maxLines="1" />
   </com.google.android.material.textfield.TextInputLayout>

      <com.google.android.material.textfield.TextInputLayout
          style="@style/TextInputLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="Password"
          android:layout_margin="15dp">

         <com.google.android.material.textfield.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:inputType="textPassword"
             android:maxLines="1" />
      </com.google.android.material.textfield.TextInputLayout>



   </LinearLayout>

</RelativeLayout>

MainActivity.java

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
deep gothi
  • 31
  • 3
3

If you are using com.android.support:design:28.0.0 or newer, the classes android.support.design.widget.TextInputLayout and android.support.design.widget.TextInputEditText no longer exist in the library.

If you are using API level 28 (or newer), you must use the com.google.android.material:material library, as other answers here describe.

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
2

I got the same error because I changed the dependencies in Gradle Build to

implementation 'com.google.android.material:material:1.2.1'

then I revert it back to

> implementation 'com.google.android.material:material:1.0.0'

and the error disappears.

Atul Yadav
  • 174
  • 1
  • 3
1

This error is thrown when
"app:passwordToggleEnabled="true" or app:passwordToggleTint="#fff" is in com.google.android.material.textfield.TextInputLayout.

By removing, it works.

It can be set programmatically:

TextInputLayout input_password_layout=(TextInputLayout)findViewById(R.id.input_password_layout);
    input_password_layout.setPasswordVisibilityToggleEnabled(true);
    input_password_layout.setPasswordVisibilityToggleDrawable(R.drawable.back);
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Beulah Ana
  • 360
  • 2
  • 14
1

a had the same situation. the reason was in "zoo" in my dependencies ))

17.07.2019 - correct list:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
Tswet
  • 71
  • 1
  • 5
1

Just Go to app > res > values > styles.xml and change the theme to:"Theme.MaterialComponents.Light.DarkActionBar" It will looks like this-

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
UTTAM
  • 319
  • 3
  • 4
1

if you are using regular style in project and you are using material component then use updated style

   // Old Style
  <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:fontFamily">@font/roboto_regular</item>
</style>

  // Updated Style

 <style name="AppTheme.NoActionBar" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:fontFamily">@font/roboto_regular</item>
</style>

Just small change and you will get result...

Sarthak Dhami
  • 330
  • 1
  • 5
  • 14
1

Add this line to your styles file, edit style name to match yours

<style name="Theme.MyAppName" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

Also ensure the activity in the manifest file theme points to

android:theme="@style/Theme.MyAppName"
Andrew Ananda
  • 90
  • 1
  • 3
1

To add to this I had the same error appear recently without any apparent code changes.

Turns out my component had a theme on the TextInputEditText that was missing the following two items:

<item name="android:textColorHighlight">@color/colorPrimary</item>
<item name="android:textColorLink">@color/colorPrimary</item>
Jack Dewhurst
  • 349
  • 3
  • 8
0

you should add

 implementation 'com.github.material-components:material-components-android:1.0.0-rc01'

to your build.gradle app level in dependencies section so it will look like:

  dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.github.material-components:material-components-android:1.0.0-rc01'
    }  
Badran
  • 515
  • 1
  • 7
  • 21
0

In your dependencies add:

implementation 'com.android.support:design:27.0.0'

Hope it helps you.. try it ..

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
0

After following all these steps listed here, I was still facing the same render problem, but I decided to actually launch the app on the device to see what happens,

And even though the preview in Android Studio was displaying the material component as a regular EditText and showing multiple warnings, it was rendered correctly on the actual device.

So to save people googling this error some time (for me it was an hour wasted), just try it on the device!

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
0

This error happened to me when in my app level dependencies I was using material design of version 1.2.0-alpha06, that's the new version. I had to put it back to version 1.0.0-beta01 and it worked well.

0

Try this

    <style name="FloatingTextStyle" parent="Widget.Design.TextInputLayout">
        <!-- Hint color and label color in FALSE state -->
        <item name="android:textColorHint">@android:color/darker_gray</item>
        <item name="android:textSize">20sp</item>
        <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
        <item name="colorAccent">@color/colorPrimaryDark</item>
        <item name="colorControlNormal">@color/colorPrimaryDark</item>
        <item name="colorControlActivated">@color/colorPrimaryDark</item>
    </style>
Chethana Arunodh
  • 335
  • 3
  • 15
0

Don't forget to also change in the manifest file on all activities with style to Theme.MaterialComponents

Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23
0

This issue is just because of Theme selection.

We can use Theme.MaterialComponents.DayNight.DarkActionBar in the XML file

For example : android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
0

Change the style of your app in the styles.xml file to this:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
    <!-- Customize your theme here. -->

</style>

This worked for me

Mixdor
  • 11
  • 1
0

add build gradle dependency as :

implementation 'com.google.android.material:material:1.5.0'

for theme.xml or style you should have something like this

<style name="NoActionBarTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
0

In my case this was because I was trying to set textColorHint to a custom attribute defined in attrs.xml. I don't know why, but that fails in Material Components version 1.5.0. After removing that bit, it worked again.

anon37894203
  • 431
  • 4
  • 17
0

I had the same problem. The error was caused by this line: app:startIconTint="@color/material_dynamic_neutral30

After removing it the error disappeared.

TextInputLayout:

        <com.google.android.material.textfield.TextInputLayout
            ...
            app:startIconTint="@color/material_dynamic_neutral30">

            <com.google.android.material.textfield.TextInputEditText
                ... />

        </com.google.android.material.textfield.TextInputLayout>
RasulOs
  • 41
  • 4
0

You are using Material Components in the application. And for that, you should also change your theme to

style name="AppTheme"> parent="Theme.MaterialComponents.DayNight.NoActionBar">

Also add this dependency to your Build.Gradle (Module) in Gradle Script Section as

implementation 'com.google.android.material:material:1.7.0'