Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.support.design.button.MaterialButton throws InflateException

I am trying to follow the The MDC Codelab on codelabs.developers.google.com with the latest library versions. The EditText -android.support.design.widget.TextInputEditText seems to work as expected however upon using the android.support.design.button.MaterialButton a runtime InflateException is thrown.

Gradle config:

compileSdkVersion 28

minSdkVersion 21

targetSdkVersion 28

dependencies {
   def lifecycle_version = "1.1.1"
   def nav_version = "1.0.0-alpha05"
   def work_version = "1.0.0-alpha06"
   def supportLibraryVersion = "28.0.0-rc01"

   implementation fileTree(include: ['*.jar'], dir: 'libs')
   api "android.arch.lifecycle:extensions:$lifecycle_version"
   api "android.arch.lifecycle:common-java8:$lifecycle_version"
   api "android.arch.navigation:navigation-fragment:$nav_version"
   api "android.arch.navigation:navigation-ui:$nav_version"
   api "android.arch.work:work-runtime:$work_version"

   api ("com.android.support:appcompat-v7:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })
   api ("com.android.support:design:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })
   api ("com.android.support:cardview-v7:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })
   api ("com.android.support:customtabs:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })

...

StackTrace

    E/XXXXXXApp: Unhandled Exception - Application Crash
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXXXXX/com.XXXXXX.activities.XXXXXXActivity}: android.view.InflateException: Binary XML file line #84: Binary XML file line #84: Error inflating class android.support.design.button.MaterialButton
    at com.XXXXXXActivity.onCreate(LoginActivity.java:107)
...

 Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
                 at android.support.design.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:170)
                 at android.support.design.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:75)
                 at android.support.design.button.MaterialButton.<init>(MaterialButton.java:140)
                 at android.support.design.button.MaterialButton.<init>(MaterialButton.java:133)

EDIT: Added more details to stacktrace and figured out that the problem was to do with the fact that the App Theme needs to be inherited from Theme.MaterialComponents.

like image 291
Nabster Avatar asked Aug 14 '18 12:08

Nabster


3 Answers

Ensure that the App Theme style is inherited from Theme.MaterialComponents

Example:

<style name="XXXXXAppTheme" parent="@style/Theme.MaterialComponents.Light.DarkActionBar"> 
like image 145
Nabster Avatar answered Oct 29 '22 00:10

Nabster


If you cannot (yet) change the theme for the whole app you can also do it just for this view:

<com.google.android.material.button.MaterialButton
    android:id="@+id/fooButon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:fontFamily="sans-serif"
    android:padding="8dp"
==> android:theme="@style/Theme.MaterialComponents.Light"
    app:backgroundTint="@color/base_white" />
like image 38
Ciprian Avatar answered Oct 28 '22 22:10

Ciprian


Apart from adding Theme.MaterialComponents

1)you can also add Theme.MaterialComponents.Bridge if you only want to get atrributes and not default styling

2)Add androidx and com.google.android.material libraries instead of changing the themes.

More info can be found (5) The Components of Material Design (Android Dev Summit '18) - YouTube at 3:25 min

like image 43
TechAJ Avatar answered Oct 28 '22 22:10

TechAJ