34

I am in the process of replacing the buttons in my app with Material Buttons using <com.google.android.material.button.MaterialButton in the XML file and private MaterialButton dateButton; in the Java fragment file. I studied the Code Lab "MDC-101 Android: Material Components (MDC) Basics (Java)" to see how a Material Button is used. In the gradle.build (Module:app) file I added the dependencies like in the code lab. The code lab compiles and runs fine. My app compiles fine but gives an error when inflating the fragment layout:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: nl.kl_solutions.schedulecompareforzermelo, PID: 16708
              android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class com.google.android.material.button.MaterialButton
              Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.google.android.material.button.MaterialButton
              Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.material.button.MaterialButton" on path: DexPathList[[zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/base.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_dependencies_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_resources_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_0_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_1_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_2_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_3_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_4_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_5_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_6_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_7_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_8_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/lib/x86_64, /system/lib64]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)

This is a snippet from my layout file:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/primaryLightColor"
    android:paddingLeft="@dimen/rasterleftpadding"
    android:paddingRight="@dimen/rasterrightpadding" >

    <Button
        android:id="@+id/btn_previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left"
        android:text="prev" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_week"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
         />

    <Button
        android:id="@+id/btn_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:text="next" />

</LinearLayout>

And here is a snippet from the onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View fragmentLayout = inflater.inflate(R.layout.fragment_week_schedule, container, false);
    //get references to the buttonBar buttons.
    leftButton = fragmentLayout.findViewById(R.id.btn_previous);
    rightButton = fragmentLayout.findViewById(R.id.btn_next);
    dateButton = fragmentLayout.findViewById(R.id.btn_week);

I only changed the datebutton to a Material Button which gives me the error. The app runs fine when using an ordinary Button for the dateButton.

This is my gradle.build file:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:appcompat-v7:$rootProject.supportVersion"
implementation "com.android.support:preference-v7:$rootProject.supportVersion"
implementation "com.android.support:recyclerview-v7:$rootProject.supportVersion"
implementation "com.android.support:cardview-v7:$rootProject.supportVersion"
implementation "com.android.support.constraint:constraint-layout:1.1.3"
implementation "com.android.support:design:$rootProject.supportVersion"
implementation "com.android.support:support-v4:$rootProject.supportVersion"
implementation 'com.google.code.gson:gson:2.8.5'
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'

//database components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"

// Lifecycle components
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
//QR library
implementation 'me.dm7.barcodescanner:zxing:1.9.8'

where $rootProject.supportVersion is 28.0.0-beta01. I know that rc-01 and rc02 for some libraries are available but I ran the code lab with the beta01 dependencies so decided to keep this version to minimize changes.

Anybody knows what is causing the runtime error?

4
  • It looks like a multiDex error, take a look at : developer.android.com/studio/build/multidex
    – Bubu
    Commented Aug 31, 2018 at 8:52
  • Thanks but no cigar. Changed the minSdkVersion to 21 and enabled multiDex but the error remains. I think that the library for the Material Button is somehow not included during compilation, but I don't know how to solve that
    – KvdLingen
    Commented Aug 31, 2018 at 9:32
  • Would you please try with 27 API. Commented Aug 31, 2018 at 9:55
  • No need. The correct answer is stated below
    – KvdLingen
    Commented Aug 31, 2018 at 10:07

7 Answers 7

73

As it suggest HERE you have to add a dependency in your build.gradle:

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

Or if you already use google support design library you must change your app theme to inherit from a Material Components theme

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->

If you cannot change your theme to inherit from a Material Components theme, you can inherit from a Material Components Bridge theme.

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->

How Reyske said:

note that it is out of beta now! So you can use implementation 'com.google.android.material:material:1.0.0'

5
  • 1
    Thanks for pointing me in the right direction. I used the widget definition form the material pages but included the dependencies from the CodeLab. They do not mix. As I have a lot of support elements I had to change the widget in the XML file to "android.support.design.button.MaterialButton" which solved this error and produced another one :).
    – KvdLingen
    Commented Aug 31, 2018 at 10:07
  • @KvdLingen I suggest you to use the classic buttons and in some cases the appCompact buttons, and set the style/themes to material, here is a really good example: stackoverflow.com/questions/26346727/…
    – TOvidiu
    Commented Aug 31, 2018 at 11:13
  • I will use Material buttons as those can easily be rounded. Solved the error anyway.
    – KvdLingen
    Commented Aug 31, 2018 at 12:35
  • note that it is out of beta now! So you can use implementation 'com.google.android.material:material:1.0.0'
    – Hibbem
    Commented Sep 25, 2018 at 9:23
  • Can the ClassNotFoundException really come up from the wrong theme definition? I'm having a similar issue with com.google.android.material.slider.Slider (other material components are working fine) (edit: the material version was the issue) Commented Jan 13, 2020 at 20:15
12

In my opinion, the best choice is to create a separate own style to the material button where it extends the material theme:

<style name="MatButton" parent="Theme.MaterialComponents">
    <item name="colorOnPrimary">@android:color/holo_red_light</item>
    <item name="colorPrimary">@android:color/holo_orange_dark</item>
    <item name="colorOnSurface">@android:color/holo_orange_light</item>
</style>

<com.google.android.material.button.MaterialButton
    android:id="@+id/searchBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Search"
    android:textColor="@android:color/white"
    android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
    android:theme="@style/MatButton"
    />
3
  • 1
    This should be the correct answer. Well done sir! I like this new style, it makes things very easy to do without wrecking all the other app styles when you are retro fitting it into an existing app with this requirement
    – Sam
    Commented Jun 14, 2019 at 17:06
  • 1
    Yes, that worked! Don't know why, but other MaterialButtons worked without the android:theme attribute... Commented Jan 17, 2020 at 13:33
  • This answer is not wrong, but if one should have to put multiple MaterialButtons, then in every block textAppearance need to be written. Instead of this you can do This Commented Aug 24, 2021 at 6:19
8

if you are using material theme component in your app, then try this.

make changes in your styles.xml from before to after ->

before:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.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>
</resources>

after:

<resources>
    <!-- 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>
    </style>
</resources>
7

When crashing The following message was output to Logcat.

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).

Therefore, you can also display MaterialButton by adding TextAppearance.

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@android:string/ok"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
0
4

Try to update your app theme to inherit from Theme.MaterialComponents (or a descendant)

1

Sometimes; if you are creating a library to be used in some other applications, you can not tell applications to change their theme. Instead, you can add the required theme(android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar") to your local layout files without any trouble as shown below:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/primaryLightColor"
android:paddingLeft="@dimen/rasterleftpadding"
android:paddingRight="@dimen/rasterrightpadding"
android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar">
...
</LinearLayout>

This should solve the ClassNotFoundException.

0

You should use textAppearance and your font style instead of fontFamily & textSize

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