[MaterialAlertDialogBuilder] Crash building dialog with Material Components app theme
See original GitHub issueDescription: Attempting to build a AlertDialog using the MaterialAlertDialogBuilder causes the app to crash in a AppCompatActivity, even though the application theme is set to a Material Components theme.
Stacktrace
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 ninja.hassie.uni.todo.MainActivity$2.handleFault(MainActivity.java:69)
at com.backendless.Persistence$1.handleFault(Persistence.java:189)
Expected behavior: The dialog should build as the app theme is set to a Material Components theme.
Source code: styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorSecondary">@color/colorAccent</item>
</style>
</resources>
MainActivity.java
public class MainActivity extends AppCompatActivity {
...
new MaterialAlertDialogBuilder(getApplicationContext())
.setMessage("Successfully added task")
.setPositiveButton(android.R.string.ok, null)
.show();
Android API version: 9 Pie
Material Library version: 1.2.0-alpha04
Device: OnePlus 5T
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Using MaterialAlertDialogBuilder with a material theme
MaterialAlertDialogBuilder requires that your application use a Material Components theme (e.g., Theme.MaterialComponents.Light).
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 >Creating a Custom Alert Dialog with Material Design library in ...
Change the app theme to inherit from a Material Components theme. This is important, else you will face a crash at runtime stating...
Read more >Alertdialog Not Showing Items With Material Component Theme
Learn how to create Alert Dialog using Kotlin in Android apps. There are three core components that build an Alert Dialog. R.string.no, Toast....
Read more >Visual Feedback: Dialogs, Snackbars and Toasts
The builder creates an Android AlertDialog using any defined color or shape in the current app theme, as long as the app uses...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @hassieswift621 - The application’s context isn’t guaranteed to have the theme, so you’ll either need to call setTheme on it or use the Activity’s context.
Unfortunately neither solution works for material design 3.0.0. Tried explicitly setting theme with setTheme on application context (which was also set in the Manifest file before the issue), and specifying the activity for the context.
This is still open and discussed here: https://github.com/material-components/material-components-android/issues/2056.
The only hacky workaround for us at the moment is extending from
Theme.MaterialComponents.Dialog.Alert
instead ofThemeOverlay.MaterialComponents.MaterialAlertDialog
and using colour references instead ofattr
from theme.