Dialog skinning with AppCompat-v7 22 results in ugly shadows on api < 21

I’m using AppCompat to write a material design styled app. Since AppCompat does not affect dialogs, I’m skinning the dialogs as such:

styles.xml:

  • Left-right arrow indicators over a ViewPager
  • Will GCM Network Manager work on non-Play devices?
  • ViewPager not working if set layout_height = wrap_content
  • CalendarView Android Help
  • Setting EditText imeOptions to actionNext has no effect
  • How to start activity in another application?
  • <style name="AppTheme.Base" parent="Theme.AppCompat">
        <!-- Set AppCompat’s color theming attrs -->
        <item name="colorPrimary">@color/green</item>
        <item name="colorPrimaryDark">@color/green_darker</item>
        <item name="colorAccent">@color/accent</item>
    
        <item name="android:alertDialogTheme">@style/alertDialog</item>
        <item name="android:dialogTheme">@style/alertDialog</item>
    </style>
    
    <style name="alertDialog" parent="Theme.AppCompat.Dialog">
        <item name="colorPrimary">@color/green</item>
        <item name="colorPrimaryDark">@color/green_darker</item>
        <item name="colorAccent">@color/accent</item>
    </style>
    

    I am gettings exactly what I wanted on android api >= 21, but on other devices I end up with a “box” around the dialogs.

    Is there a way to get rid of the “box” around the dialog and even get the colors and material theme applied on api < 21, preferably without any additional depencendies?

    App on Api < 21:

    App on API < 21

    App on API >= 21:

    App on API >= 21

    Related posts:

    Why am I getting a null reference on my RecyclerView
    Theme.AppCompat.Light.DarkActionBar - No resource found
    howto in eclipse debug the Proguard obfuscated apk file
    How to replace strings resources with Android Gradle
    Good examples of non-game Marmalade apps?
    Android programatic button font size in pixels
  • CoordinatorLayout with two floating action buttons
  • Clearing intent
  • Wake locks android service recurring
  • Signing an app, but not publishing on Android market
  • Using android vector Drawables on pre Lollipop crash
  • Back button closing app even when using FragmentTransaction.addToBackStack()
  • 5 Solutions collect form web for “Dialog skinning with AppCompat-v7 22 results in ugly shadows on api < 21”

    With the new AppCompat v22.1 you can use the new android.support.v7.app.AlertDialog or the new AppCompatDialog

    Just use a code like this (of course in your case you have to use a custom layout to have the progress bar)

    import android.support.v7.app.AlertDialog
    
    AlertDialog.Builder builder =
           new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
                builder.setTitle("Dialog");
                builder.setMessage("Lorem ipsum dolor ....");
                builder.setPositiveButton("OK", null);
                builder.setNegativeButton("Cancel", null);
                builder.show();
    

    And use a style like this:

    <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
            <item name="colorAccent">#FFCC00</item>
            <item name="android:textColorPrimary">#FFFFFF</item>
            <item name="android:background">#5fa3d0</item>
        </style>
    

    In case anyone is still looking for a simple and effective solution, just add this line to your “alertDialog” style:

    <item name="android:windowBackground">@android:color/transparent</item>
    

    P.S. changing this property also affects PreferenceFragment’s dialogs on API >= 21, so make sure you are using different styles: with transparent bg for API < 21, and without any changes for API >= 21

    I had the exact same problem with the new AppCompat 22 library when using android.support.v7.app.AlertDialog for all my AlertDialogs, but the extra background layers only affected ProgressDialogs.

    All my AlertDialogs when styled using my custom theme looked great, but the ProgressDialog’s had the weird overlay on the background as described in the OP.

    An option I had was to set a specific style each time I constructed a ProgressBar, but I was looking for an application wide solution.

    With the help of these two links:

    How to Style AlertDialogs like a Pro and Joerg Richter Blog I was able to get rid of the extra layer drawn on < 21 ProgressDialogs.

    The issue I found was that on all versions the ProgressBar draws its background based on what is defined by default in “android:alertDialogStyle”.

    So to get rid of the extra layer, I had to define my own styles and set them for “android:alertDialogStyle”. In doing so, I’m also overriding the default layouts applied to ProgressDialogs.

    Here’s my themes.xml:

    <item name="android:alertDialogTheme">@style/MyAlertDialogTheme</item>
    <item name="alertDialogTheme">@style/MyAlertDialogTheme</item>
    <item name="android:alertDialogStyle">@style/MyAlertDialogStyles</item>
    

    And my styles.xml:

    <style name="MyAlertDialogTheme">
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
        <item name="android:background">@color/theme_alert_dialog_background</item>
        <item name="colorAccent">@color/theme_accent_1</item>
    </style>
    
    <style name="AlertDialog">
        <item name="android:fullDark">@android:color/transparent</item>
        <item name="android:topDark">@android:color/transparent</item>
        <item name="android:centerDark">@android:color/transparent</item>
        <item name="android:bottomDark">@android:color/transparent</item>
        <item name="android:fullBright">@android:color/transparent</item>
        <item name="android:topBright">@android:color/transparent</item>
        <item name="android:centerBright">@android:color/transparent</item>
        <item name="android:bottomBright">@android:color/transparent</item>
        <item name="android:bottomMedium">@android:color/transparent</item>
        <item name="android:centerMedium">@android:color/transparent</item>
    </style>
    

    ProgressDialog – Try for below android 5

    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    

    or

    dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
    

    You may download revision 22.1.0 (updated a few days ago) and use android.support.v7.app.AlertDialog

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