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.

[MaterialAlertDialogBuilder] Crash building dialog with Material Components app theme

See original GitHub issue

Description: 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:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
ikim24commented, Feb 3, 2020

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.

0reactions
nimaiwalshcommented, Mar 28, 2021

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 of ThemeOverlay.MaterialComponents.MaterialAlertDialog and using colour references instead of attr from theme.

Read more comments on GitHub >

github_iconTop 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 >

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