Nabster
Nabster

Reputation: 1227

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.

Upvotes: 24

Views: 18822

Answers (5)

VIVek
VIVek

Reputation: 141

if you don't want to make changes to the app's whole theme and answer no. 2 makes your button disappear, then I suggest adding this to your MaterialButton

android:theme="@style/Theme.MaterialComponents.NoActionBar"

Upvotes: 0

0101100101
0101100101

Reputation: 5911

The error may also occur when using any Material Components versions starting from 1.0.0 up to the latest, caused by a theme naming conflict because of themes specified in external libraries, such as Mobile FFmpeg. In this case you need to change the name of your own theme. For further information I explain this in more detail in an answer to this similar question: https://stackoverflow.com/a/57565773/2964379

Basically if you encounter this problem, but know that you are correctly using a MaterialComponents theme, do a search in Android Studio for your app theme using "Find in Path" -> "Scope" -> "All Places". For example, after building and running your app, if your theme name is AppTheme, search for "AppTheme" including the quotes to see if there is a cached XML file that specifies the exact same name.

Upvotes: 1

Nabster
Nabster

Reputation: 1227

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

Example:

<style name="XXXXXAppTheme" parent="@style/Theme.MaterialComponents.Light.DarkActionBar"> 

Upvotes: 59

Ciprian
Ciprian

Reputation: 2899

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" />

Upvotes: 33

Apoorva Jain
Apoorva Jain

Reputation: 475

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

Upvotes: 1

Related Questions