How can I change the background of Android alert dialogs?

I use the AlertDialog class in my application. By default, these alert dialogs have a transparent background. I’m trying to use an opaque background instead, very unsuccessfully. These are my styles:

<style name="MyOpaqueActivity" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@drawable/my_background</item>
    <item name="android:alertDialogStyle">@style/MyOpaqueAlertDialog</item>
</style>

<style name="MyOpaqueAlertDialog" parent="@android:style/Theme.Dialog.Alert">
    <item name="android:background">#454545</item>
    <item name="android:windowBackground">@drawable/my_background</item>
    <item name="android:popupBackground">@drawable/my_background</item>
</style>

I applied the “MyOpaqueActivity” style successfully for whole activities (the window background is changed to “my_background”), but it doesn’t work for alert dialogs within those activities. The “alertDialogStyle” attribute and my “MyOpaqueAlertDialog” style don’t seem to have any effect.

  • Download image from new Google+ (plus) Photos Application
  • WebRTC on Android
  • Why do I get “Failed to bounce to type” when I turn JSON from Firebase into Java objects?
  • How to check permission is granted for a directory path and won't thorow EACCES error?
  • ViewPager + FragmentStatePagerAdapter + orientation change
  • SQLite database on SD card
  • So how can I change the background of these alert dialogs?

    Related posts:

    Tap outside edittext to lose focus
    How can a button get the focus?
    List of files of saved to the internal storage
    Android AdMob causes memory leak?
    Is there any way to reduce the boot up time of Android Emulator provided with android SDK
    GZIP compression to a byte array
  • Where appears android:description label from your manifest?
  • Load image from url
  • Is there a way to force Google Speech api to return only words as response?
  • How to define a screen-size specific animation in XML
  • How to create persistent alarms even after rebooting
  • positioning a compoundDrawable within TextView?
  • 3 Solutions collect form web for “How can I change the background of Android alert dialogs?”

    Your approach won’t work. It seems AlertDialog (and Builder) hardcode the theme and don’t honor alertDialogStyle anywhere:

    protected AlertDialog(Context context) {
        this(context, com.android.internal.R.style.Theme_Dialog_Alert);
    }
    
    public Builder(Context context) {
        this(context, com.android.internal.R.style.Theme_Dialog_Alert);
    }
    

    They came to the same conclusion here.

    A custom dialog class derived from AlertDialog that calls the protected constructor AlertDialog(context, R.style.MyOpaqueAlertDialog) would be a workaround.

    In the latest android source, there is a new public constructor for AlertDialog.Builder that takes a theme argument. Unfortunately, it hasn’t been released yet (maybe in Gingerbread?).

    It’s easy to do this in an XML style once you figure that AlertDialogs have somewhat special attributes.

    However, it would be interesting to know which Android version you’re referring to (or are targeting at). Theme.Dialog is relatively new as far as I know. Newer Android versions also don’t use AlertDialogs under all circumstances where older versions used them.

    This works for me in Android 2.2:

    <style name="MyTheme" parent="@android:style/Theme">
        <item name="android:alertDialogStyle">@style/MyOpaqueAlertDialog</item>
    </style>
    
    <style name="MyOpaqueAlertDialog">
        <item name="android:fullDark">@drawable/[...]</item>
        <item name="android:topDark">@drawable/[...]</item>
        <item name="android:centerDark">@drawable/[...]</item>
        <item name="android:bottomDark">@drawable/[...]</item>
        <item name="android:fullBright">@drawable/[...]</item>
        <item name="android:topBright">@drawable/[...]</item>
        <item name="android:centerBright">@drawable/[...]</item>
        <item name="android:bottomBright">@drawable/[...]</item>
        <item name="android:bottomMedium">@drawable/[...]</item>
        <item name="android:centerMedium">@drawable/[...]</item>
    </style>
    

    Newer Android versions also have android:progressLayout and android:horizontalProgressLayout attributes for the AlertDialog style.

    Also, in newer Android versions, it is possible to refer to an AlertDialog theme by using alertDialogTheme instead of alertDialogStyle in a custom theme. AlertDDialogThemes support more familiar and more powerful attributes like windowBackground, windowTitleStyle etc. Have a look at styles.xml and themes.xml.

    Unfortunately, the documentation of which feature was added when is super poor. Please find out for yourself whcih approach is the best for you.

    From the documentation its looks like you could do this in onCreateDialog.

    FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
    fl.setBackgroundResource(); // has to be a drawable.
    

    Only other solution is a custom dialog using that Theme.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.