'Error inflating class com.google.android.material.button.MaterialButton

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 :

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



Solution 1:[1]

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.

Solution 2:[2]

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

    <application
        android:theme="@style/AppTheme"
        ...>

Solution 3:[3]

change your theme

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

to

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriele Mariotti
Solution 2 i30mb1
Solution 3 Elfnan Sherif