0

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.

  • 1
    Does this answer your question? [Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?](https://stackoverflow.com/questions/5436822/why-does-alertdialog-buildercontext-context-only-accepts-activity-as-a-paramet) – Hayk Mkrtchyan Apr 01 '22 at 10:22
  • it could be a solution but I should handle the event that shows the dialog in each activity – Daniele Soprani Apr 01 '22 at 11:19
  • The `android:theme` on an `` only sets the default theme for the `Activity` classes; it is not set on anything else, like the `Application` or `applicationContext`. You could ostensibly fix the current issue with a `ContextThemeWrapper`, but that's only going to make way for the next one, which is described in the link Hayk gave above. Normal apps can only show `Dialog`s with an `Activity`'s `Context`. Notifications would probably be the preferred alternative here, but you could also use a `Dialog`-themed `Activity`, if you really want to show this from an `Application`. – Mike M. Apr 01 '22 at 13:38

1 Answers1

0

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.

Joey
  • 21
  • 10
  • That can be a solution but I want to use Material3 as parentTheme – Daniele Soprani Apr 01 '22 at 11:21
  • Are you passing the Context like: "YourActivity.this"? And have you tried to just use Activity instead of AppCompat? – Joey Apr 01 '22 at 12:14
  • I'm passing context like `MyDialog(this.applicationContext)`. Yes, I tried using Activity – Daniele Soprani Apr 01 '22 at 14:04
  • For now, i can only reference to the answer from Mike M. above: You can try it with: https://stackoverflow.com/a/30181104/7173050 – Joey Apr 01 '22 at 14:24
  • I tried but I'm getting this error `android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?` – Daniele Soprani Apr 01 '22 at 14:43
  • I think, thats because you use application context for your Dialog, and not activity context. So instead of getApplicationContext() or this.applicationContext, you have to use YourActivity.this – Joey Apr 04 '22 at 06:40