Error inflating class com.google.android.material.button.MaterialButton
Asked Answered
P

4

10

What i'm trying to achieve is having a gridview with some materialButton inside. I tried to create the gridview like :

<GridView
            android:id="@+id/login_gridview_code_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="3"
            android:horizontalSpacing="15dp"
            android:verticalSpacing="15dp">
        </GridView>

And the element to be inflated like:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/button_code_digit"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            android:padding="0dp"
            app:iconGravity="textStart"
            app:iconPadding="0dp"
            app:iconSize="40dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.pswStorer.Button.Circle" />

</androidx.constraintlayout.widget.ConstraintLayout>

the adapter inflates the button :

inflter = (LayoutInflater.from(applicationContext)); 
view = inflter.inflate(R.layout.button_code, null); // inflate the layout

but I always receive this stacktrace :

Error inflating class com.google.android.material.button.MaterialButton
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

I checked the appTheme but seems correct to me:

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

Any ideas?

EDIT: I tried to check and my app has the correct theme

android:theme="@style/AppTheme"

I also changed the theme to Theme.MaterialComponents.DayNight.NoActionBar.Bridge but nothing changed

Pina answered 4/5, 2022 at 10:22 Comment(3)
How do you get inflter?Broadleaved
@GabrieleMariotti inflter = (LayoutInflater.from(applicationContext));Pina
You can find the answer below. The issue is the application context. it doesn't have the app theme.Broadleaved
B
5

The issue is here.

inflter = (LayoutInflater.from(applicationContext)); 

The Application context doesn't have your app theme.
You have to use a themed context like an Activity.

Broadleaved answered 9/5, 2022 at 9:42 Comment(2)
thanks, using the context of an activity was the fixPina
@gabriele-mariotti Can you provide more details for this, and can we declare it in style itself?Cheese
C
8

change your theme

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

to

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
Cymophane answered 4/5, 2022 at 11:31 Comment(2)
tried but same resultPina
This worked for me. I switched to MaterialButton but my theme was still default.Senhor
B
5

The issue is here.

inflter = (LayoutInflater.from(applicationContext)); 

The Application context doesn't have your app theme.
You have to use a themed context like an Activity.

Broadleaved answered 9/5, 2022 at 9:42 Comment(2)
thanks, using the context of an activity was the fixPina
@gabriele-mariotti Can you provide more details for this, and can we declare it in style itself?Cheese
L
0

Check your AndroidManifest.xml that you are using right theme for your app also

    <application
        android:theme="@style/AppTheme"
        ...>
Luane answered 4/5, 2022 at 10:49 Comment(0)
D
0

If you've got a separate theme for night don't forget to change to the Theme.MaterialComponents theme there too.

Doubleness answered 15/9, 2022 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.