Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

<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

like image 264
snowdragon Avatar asked Apr 13 '15 14:04

snowdragon


5 Answers

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>
like image 179
Gabriele Mariotti Avatar answered Oct 23 '22 07:10

Gabriele Mariotti


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

like image 38
Alex Timonin Avatar answered Oct 23 '22 05:10

Alex Timonin


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>
like image 39
activelogic Avatar answered Oct 23 '22 06:10

activelogic


ProgressDialog - Try for below android 5

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

or

dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
like image 36
Rahul Avatar answered Oct 23 '22 06:10

Rahul


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

like image 2
hoonj Avatar answered Oct 23 '22 05:10

hoonj