[MaterialAlertDialog] The style on this component requires your app theme to be Theme.AppCompat
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
Don't know what happened with 1.3.0 but I'm getting
The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
All over the place on MaterialAlertDialogBuilder and some other Material Components, some of them crashes at rendering the starting activity making the application useless causing me bad ratings and uninstalls. Been using the library for almost 2 years now.
The issue is worst because I can't debug since it happens in a few devices that I do not own. I'm refactoring going blind here, only based on guesses....
One of the users using a Xiaomi was kind enough to share a screenshot of one of the issues, on the left you can see the screen after updating the app to Material Library 1.3.0 on the right you can see the expected appearance. The user also reported that after removing and reinstalling the app the appearance went back to normal.

Hi! I had the same problem.
After much trial and error, I found that extending Theme.MaterialComponents.Dialog.Alert instead of ThemeOverlay.MaterialComponents.MaterialAlertDialog worked for me.
But then you can't use attributes from your Theme, I don't think (e.g. ?attr/colorPrimarySurface). You would have to use styles instead of attributes (e.g. @color/colorPrimarySurface)
Not sure if this solves your problem as well...
Also encountered this issue with com.google.android.material:material:1.3.0. Extending Theme.MaterialComponents.Dialog.Alert is not an option for us as we require the extra attributes.
Reverting back to 1.3.0-beta01 fixes the issue, but it's a pretty off bug in a mainstream "stable" version. There are newer versions than 1.3.0 but these are all experimental ones.
Same issue .Still its reproducing
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 😅😅😅
@algerico-abad Thanks, man. Your solution worked for me too.
[OFF-TOPIC] as you said you are a beginner I want to give you an advise. There is no reason to use Java on Android development today. The official language is Kotlin since 2017. Most codes are written in Kotlin, including libs and google documentation. Try to migrate your code to Kotlin as soon as you can. Android Studio has great features to help you on this conversion (just copy your java code to a whatsoever.kt file and see the magic)
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()toActivityName.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 😅😅😅
Thank you! It worked for me.
I know it's a bit confusing but the issue has nothing to do with materialAlertDialogTheme but the context you are using to create the dialog.
Please make the context you are using in MaterialAlertDialogBuilder's constructor is correctly themed with Theme.AppCompat or its descendant.
I had this problem due to the fact that I used injected context (ApplicationContext) from dagger hilt
I had this problem and fixed it by changing the theme "Theme.MaterialComponents.Light.NoActionBar" with "Theme.MaterialComponents.Light.NoActionBar.Bridge" and this works for me.
I also had this issue, but finally fixed it.
if ?attr/colorPrimary or any of the attribute are not defined, you will get the issue. As this is a theme overlay, I swapped the ?attr/colorPrimary references out with @colors/colorPrimary and it worked.
<style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="colorPrimary">?attr/colorPrimary</item>
<item name="colorSecondary">?attr/colorSecondary</item>
<item name="colorSurface">?attr/colorSurface</item>
<item name="colorOnSurface">?attr/colorOnSurface</item>
I know it's a bit confusing but the issue has nothing to do with materialAlertDialogTheme but the context you are using to create the dialog.
Please make the context you are using in MaterialAlertDialogBuilder's constructor is correctly themed with Theme.AppCompat or its descendant.
Thank You! It works for me! I changed my code from the unmatched context to this@ActivityName and it worked.
MaterialAlertDialogBuilder(this@MainActivity).
setTitle("Change Cover Photo").show()