7

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

3
  • How do you get inflter? May 6, 2022 at 16:33
  • @GabrieleMariotti inflter = (LayoutInflater.from(applicationContext));
    – Anon
    May 9, 2022 at 9:33
  • You can find the answer below. The issue is the application context. it doesn't have the app theme. May 9, 2022 at 9:43

4 Answers 4

6

change your theme

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

to

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
2
  • tried but same result
    – Anon
    May 4, 2022 at 12:24
  • This worked for me. I switched to MaterialButton but my theme was still default.
    – William
    Apr 22 at 21:52
2

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.

1
  • 1
    thanks, using the context of an activity was the fix
    – Anon
    May 9, 2022 at 10:53
0

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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