'Unable to display an AlertDialog from the android Application class

I'm trying to display programmatically an AlertDialog from the Application class using the applicationContext. I'm using my own Theme that extends Theme.Material3.Light. Only Toast messages works fine. I'm getting this error:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

My application Theme is:

android:theme="@style/Theme.MyTheme"

MyTheme is defined as:

<style name="Theme.MyTheme" parent="Theme.Material3.Light">

I have also a custom style for AlertDialogs defined in MyTheme:

<!-- AlertDialog Style-->
<style name="AlertDialogTheme" parent="MaterialAlertDialog.Material3">
    <item name="android:background">@drawable/dialog_light</item>
</style>

The class from which I launch the event

class AppController: Application()

I tried changing the default theme in the Manifest and then setting the custom one programmatically when the application in launched but it did not work.



Solution 1:[1]

It seems like your activity extends from AppCompatActivity and your theme is from Activity.

In your style.xml you have to do something like this:

<style name="ActionBarAppCompatTheme" parent="Theme.AppCompat.Light.DarkActionBar">

Or you can just change AppCompatActivity to Activity in your class.

Hope it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1