question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MaterialAlertDialog] The style on this component requires your app theme to be Theme.AppCompat

See original GitHub issue

Description: I’m having random crashes when trying to create MaterialAlertDialog. The message: The style on this component requires your app theme to be Theme.AppCompat This is happening in some devices while is not happening in others, for instance I have an A71 Android 10, and is ok, but Crashlytics reports too many so far all related to the same issue and there are also A71 reported in Crashlytics with the same issue. I’m setting a custom view into the Alert Dialog and in some instances I’m creating the Builder from a static Utility class that I can use across the application.

The source code below is from one of the cases, but I still have many issues with other Alert Dialog related to the same. This is a major issue for me, because is the source of too many crashes and I literally can’t reproduce with my devices, I’m aware of the crashes only for user reports and crashlytics.

Expected behavior: The dialog should show without any issue.

Source code:

    public void showChoiceDialog() {
        View randomGeneratorView = View.inflate(this, R.layout.alert_dialog_random_generator, null);
        TextInputEditText presidentName = randomGeneratorView.findViewById(R.id.random_presidents_picker_name);
        presidentName.setText(Utilities.getSharedPreferences(this).getString(SP_CARD_NAME, EMPTY_STRING));
        NumberPicker presidentsPicker = randomGeneratorView.findViewById(R.id.random_presidents_picker);
        presidentsPicker.setMinValue(1);
        presidentsPicker.setMaxValue(45);
        presidentsPicker.setValue(Utilities.getSharedPreferences(this).getInt(RANDOM_PICK, 1));
        new MaterialAlertDialogBuilder(this, R.style.alert_dialog_material_theme_progress_bar)
                .setTitle(R.string.generate_random_presidents_name)
                .setView(randomGeneratorView)
                .setPositiveButton(R.string.ok, (dialog, which) -> {
                    presidents.clear();
                    int randomPick = presidentsPicker.getValue();
                    generateRandomCards(randomPick, presidentName.getText() == null ? EMPTY_STRING : presidentName.getText().toString().trim());
                    Utilities.savePreferences(RANDOM_PICK, randomPick, Utilities.EDITOR_IS_INT, this);
                })
                .setNegativeButton(R.string.cancel, (dialog, which) -> {
                })
                .show();
    }

R.layout.alert_dialog_random_generator

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="@dimen/eight_dip">

    <NumberPicker
        android:id="@+id/random_president_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/presidents_picker_random"
        app:layout_constraintBottom_toTopOf="@+id/random_president_picker_name_container"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/random_president_picker_name_container" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/random_president_picker_name_container"
        style="@style/edit_text_input_layout"
        android:layout_width="@dimen/one_eighty_dip"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/eight_dip"
        android:hint="@string/cards_name"
        app:counterEnabled="true"
        app:counterMaxLength="10"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/random_president_picker">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/random_president_picker_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="10"
            android:textColor="?attr/colorPrimarySurface"
            android:textSize="@dimen/sixteen_sip" />
    </com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Theme

    <style name="theme_alpha" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="actionButtonStyle">@style/action_button_style</item>
        <item name="actionModeCloseDrawable">@drawable/nav_draw_back</item>
        <item name="actionModeStyle">@style/action_mode</item>
        <item name="android:includeFontPadding">false</item>
        <item name="bottomSheetDialogTheme">@style/bottom_sheet_dialog_theme</item>
        <item name="card_background_color">@color/blue_grey_900</item>
        <item name="card_text_color">@android:color/white</item>
        <item name="checkboxStyle">@style/checkbox_theme</item>
        <item name="fontFamily">@font/montserrat_regular</item>
        <item name="listPreferredItemHeightSmall">@dimen/forty_eight_dip</item>
        <item name="materialAlertDialogTheme">@style/alert_dialog_material_theme</item>
        <item name="snackbarButtonStyle">@style/snack_bar_button</item>
        <item name="snackbarStyle">@style/snack_bar_theme</item>
        <item name="snackbarTextViewStyle">@style/snack_bar_text_style</item>
        <item name="spinnerStyle">@style/spinner_style</item>
        <item name="windowActionModeOverlay">true</item>
    </style>

Styles

   <style name="alert_dialog_material_theme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="android:textColor">?attr/colorPrimarySurface</item>
        <item name="android:textColorPrimary">?attr/colorPrimarySurface</item>
        <item name="android:windowMinWidthMajor">@dimen/window_min_width_major</item>
        <item name="android:windowMinWidthMinor">@dimen/window_min_width_minor</item>
        <item name="colorOnSurface">?attr/colorPrimarySurface</item>
        <item name="colorPrimary">?attr/colorPrimarySurface</item>
        <item name="colorSurface">?attr/colorPrimaryVariant</item>
        <item name="shapeAppearanceOverlay">@style/alert_dialog_material_shape_appearance</item>
        <item name="windowMinWidthMajor">@dimen/window_min_width_major</item>
        <item name="windowMinWidthMinor">@dimen/window_min_width_minor</item>
    </style>

    <style name="numbers_picker_random">
        <item name="android:textColorPrimary">?attr/colorPrimarySurface</item>
        <item name="android:textSize">@dimen/twenty_sip</item>
        <item name="colorControlNormal">?attr/colorAccent</item>
    </style>

Logcat from Crashlytics

Caused by java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
       at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
       at com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:213)
       at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:148)
       at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76)
       at com.google.android.material.dialog.MaterialDialogs.getDialogBackgroundInsets(MaterialDialogs.java:60)
       at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:117)
       at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:103)
       at com.guanacodevs.utilities.Utilities.customAlertDialog(Utilities.java:411)
       at com.guanacodevs.ubv.activities.ActivityHome.resetGame(ActivityHome.java:2117)
       at java.lang.reflect.Method.invoke(Method.java)
       at androidx.appcompat.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:267)
       at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:154)
       at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
       at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:975)
       at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:623)
       at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
       at android.view.View.performClick(View.java:6659)
       at android.view.View.performClickInternal(View.java:6631)
       at android.view.View.access$3100(View.java:790)
       at android.view.View$PerformClick.run(View.java:26187)
       at android.os.Handler.handleCallback(Handler.java:907)
       at android.os.Handler.dispatchMessage(Handler.java:105)
       at android.os.Looper.loop(Looper.java:216)
       at android.app.ActivityThread.main(ActivityThread.java:7625)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

Android API version: 8, 9, 10, 11

Material Library version: Material Android Library version you are using here 1.3.0

Device: Too many to list

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
algerico-abadcommented, Jun 15, 2021

I was getting the same error when I was creating a Simple MaterialAlertDialog. This is my code:

String[] items = {"Select from Gallery"};
new MaterialAlertDialogBuilder(getApplicationContext())
        .setTitle("Change Cover Photo")
        .setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SelectImageToUpload();
            }
        }).show();

But after I change my mind, I made something else, but later I received an error in my ProgressDialog which resulted in it not displaying and it was fixed after changing the getApplicationContext() to ActivityName.this. So I thought maybe it will also fix the Simple MaterialAlertDialog. To my surprise it did. This is my code now:

String[] items = {"Select from Gallery"};
new MaterialAlertDialogBuilder(ActivityShopDetail.this)
        .setTitle("Change Cover Photo")
        .setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SelectImageToUpload();
            }
        }).show();

I’m still a noob in terms of programming in Android so feel free to criticize my code or whatever you want to criticize 😅😅😅

3reactions
MishaNikasovcommented, Jan 15, 2022

I had this problem due to the fact that I used injected context (ApplicationContext) from dagger hilt

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material Alert Dialog, The style on this component requires ...
You're passing in getApplicationContext() and the application context doesn't have your theme. Pass in your activity as the context:
Read more >
Migrating to Material Components for Android
This guide uses a simplified app to demonstrate the migration process. It uses an AppCompat theme, widgets from the Design Support Library ...
Read more >
Theming with AppCompat - Medium
When using a base theme of Theme.AppCompat and the AppCompat-provided app bar, you'll find the app bar's background automatically uses your colorPrimary.
Read more >
Android Material Components - MaterialAlertDialog
In this tutorial, we'll be customizing Dialogs using Material Theme in our Android Application. Material Components - Dialogs. Alert Dialogs are ...
Read more >
MaterialAlertDialogBuilder - Android Developers
Builder for use with a Material theme (e.g., Theme.MaterialComponents). ... From class androidx.appcompat.app.AlertDialog.Builder ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found