Error inflating class com.google.android.material.textfield.TextInputLayout
Asked Answered
C

5

22

This is my app theme: <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

I got an inflate error caused by a not using a desired theme. In the Manifest I didn't overwrite the theme for the activity that I got this error, so the AppTheme definded in styles.xml is used.

Error:

 android.view.InflateException: Binary XML file line #122: Binary XML file line #122: Error inflating class com.google.android.material.textfield.TextInputLayout
    Caused by: android.view.InflateException: Binary XML file line #122: Error inflating class com.google.android.material.textfield.TextInputLayout
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
      ...
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240)
        at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215)
        at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143)
        at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:116)

And this is my xml:

  <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/inputFirstName"
                        style="@style/EditText.OutlinedBox"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginEnd="16dp"
                        android:visibility="gone"
                        app:boxStrokeColor="@color/colorBrand"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent">

and the EditText style:

 <style name="EditText.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="android:minHeight">56dp</item>
        <item name="android:textSize">18sp</item>
        <item name="android:fontFamily">@font/nunito_regular</item>
        <item name="android:textColorHint">@color/colorTextSecondary</item>
        <item name="hintTextColor">@color/colorBrand</item>
        <item name="hintEnabled">true</item>
    </style>

target sdk of the app is 29 and I use material_design_components_version = '1.1.0-alpha08'

EDIT:

from app/gradle

    implementation(
        "androidx.appcompat:appcompat:$appcompat_version",
        "androidx.constraintlayout:constraintlayout:$constraintlayout_version",
        "com.google.android.material:material:$material_design_components_version"
)

Manifest:

<application
    android:name=".application.BaseApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

Theme:

<!-- Base application theme. -->
<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>
    <item name="android:windowLightStatusBar">true</item>
    <item name="android:fontFamily">@font/nunito_regular</item>
    <item name="android:lineSpacingExtra">0dp</item>
    <item name="android:includeFontPadding">false</item>
    <item name="navigationIcon">@drawable/ic_back_dark</item>
    <item name="android:windowBackground">@color/colorWhite</item>
    <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
Caput answered 21/7, 2019 at 14:24 Comment(9)
Add android:theme="Theme.MaterialComponents" to the layout.Kurrajong
not working, crash with the same messageCaput
Pls show us how your design reference looks like in gradle. It very much looks like you're mixing appcompat and androidx. Pls also add look of your manifest - app theme in particular.Vertu
@Vertu i have added the detailsCaput
Hm can it be that your AppTheme has different representations for different sdk versions, and is not properly inherited for version you are testing against? @CaputVertu
I dont understandm what you are reffering to @VertuCaput
I will try to rephrase: there is one impl of AppTheme under res/values/styles.xml, another under res/values-v23/styles/xml etc.Vertu
@Vertu got it now. not my case, I have only one AppTheme definedCaput
have you find a solution for that issue? I have encountered this problem and couldn't find any solution yet @CaputDispensary
L
56

Try in parent layout apply this style:

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

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical"
        android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar">
        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        <com.google.android.material.textfield.TextInputEditText
       
        </com.google.android.material.textfield.TextInputLayout>
   </LinearLayout>

This is an example,for complete using, put in your app style the parent,Exemple:

<style name="MyApp" parent="Theme.MaterialComponents">
Loni answered 4/2, 2020 at 23:12 Comment(1)
Setting a theme in the Layout can be a useful debugging tool. However, a typical application would set an application-wide theme in AndroidManifest.xml. This theme is often defined in values/themes.xml and should have a parent of Theme.MaterialComponents.DayNight.DarkActionBar (or similar).Merriemerrielle
D
7

I implemented this:

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

Then I changed the style from:

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

To:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Dependency answered 4/7, 2020 at 18:36 Comment(0)
S
4
 launchFragmentInContainer<LoginFragment>(themeResId =R.style.AppTheme)

add theme in constructor!!

Spathose answered 27/8, 2020 at 14:44 Comment(3)
This is the only thing that I had to do. Worked like a charm, thank you!Groundhog
@Groundhog you are welcome, glad to hear that it helped to someone.Spathose
Thanks for sharing the solution.Life Saver for me.Steersman
R
1

The problem is with the material version. Try changing your version alpha06 and it should fix it:

implementation 'com.google.android.material:material:1.1.0-alpha06'
Regnant answered 25/7, 2019 at 20:14 Comment(2)
no, the problem was that textinput was inside a dialog, and the context that i used for dialog was the activity context and not the fragment context, that opent the dialogCaput
This seems to work fine for me. Thanks.Other
M
1

check your theme inheritance, in my case in light theme I inherited with material component and in the dark theme from material3

Mccracken answered 28/2, 2022 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.