Progress Dialog appears weirdly on Pre-lollipop devices
Asked Answered
C

4

10

My progress dialog on pre-lollipop devices appears like this: enter image description here

See that double window? I have no clue as to why this this happening.

Code

  1. Initializing the progress dialog like this:

    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage(messsage);
    progressDialog.setIndeterminate(true);
    progressDialog.setCancelable(false);
    
  2. Defined a style like this in values and values-21:

<style name="AlertDialog.Theme" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:textColorPrimary">@color/black</item>
        <item name="android:background">@color/white</item>
        <item name="android:textColor">@color/black</item>
        <item name="colorAccent">@color/orange</item>
        <item name="colorPrimary">@color/orange</item>
        <item name="colorPrimaryDark">@color/darkerorance</item>
    </style>
  1. After some searching on google, I added the alert style in my main theme like this:

<item name="android:dialogTheme">@style/AlertDialog.Theme</item>
        <item name="dialogTheme">@style/AlertDialog.Theme</item>
        <item name="android:alertDialogTheme">@style/AlertDialog.Theme</item>
        <item name="alertDialogTheme">@style/AlertDialog.Theme</item>

And the main theme extends from Theme.AppCompat.Light.NoActionBar.

This works great on Lollipop and above, but appears like in the image on pre-lollipop. Can anyone help out here and tell me what am I doing wrong?

Candidacandidacy answered 18/12, 2015 at 14:34 Comment(2)
I don't think there's a support version of ProgressDialog.Candidacandidacy
Not sure of your particular use-case, but as a general UI consideration, you might want to consider using a ViewFlipper with a loading view (e.g., FrameLayout with ProgressDialog) instead of a ProgressDialog. ProgressDialogs are blocking...so if your loading state gets hung or takes a while, the user is blocked from taking other actions.Bradawl
C
4

I am a little late replying but this is how I got around solving it.
I created a separate v-14 styles.xml and defined a style as shown below

values-v14/styles.xml

<style name="AlertDialog.Holo" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:textAppearance">@style/Helvetica.Regular</item>
    <item name="android:windowTitleStyle">@style/DialogTitleStyle</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

P.S - textAppearance and windowTitleStyle are styles I had created for the custom fonts.

The regular (values/styles.xml) alert dialog style used is:

<style name="AlertDialog.Theme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textColor">@color/black</item>
    <item name="colorAccent">@color/zifycolor</item>
    <item name="colorPrimary">@color/zifycolor</item>
    <item name="colorPrimaryDark">@color/zifycolorDarker</item>
    <item name="android:windowTitleStyle">@style/DialogTitleStyle</item>
    <item name="buttonBarButtonStyle">@style/AlertDialogButtonStyle</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Finally to show a progress dialog:

/**
 * Create a progress dialog. The appropriate theme gets applied.
 *
 * @param context  valid context with a window
 * @param messsage message to show
 * @return {@code ProgressDialog} instance
 */
public static ProgressDialog createProgressDialog(@NonNull final Context context, @NonNull String messsage) {
    ProgressDialog progressDialog;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        progressDialog = new ProgressDialog(context, R.style.AlertDialog_Theme);
    else
        progressDialog = new ProgressDialog(context, R.style.AlertDialog_Holo);

    progressDialog.setMessage(messsage);
    progressDialog.setCancelable(false);
    return progressDialog;

}

So far it works great! Hope it helps somebody.

Candidacandidacy answered 29/1, 2017 at 5:46 Comment(0)
S
3

My workaround was the create a separate theme and style for pre-Lollipop with the parameter android:windowBackground as transparent. This removed the 2nd background in the back.

<style name="AppCompatAlertDialogStyleNoWindow" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowBackground">@color/transparent</item>
</style>

Then add this style as the android:alertDialogTheme for pre-Lollipop theme.

Sternson answered 3/6, 2016 at 23:5 Comment(1)
Only tested this API 16 and aboveSternson
N
2

Your answer is not working for pre-Lollipop (tested on API 19). But I find solution: you need to use android.support.v7.app.AlertDialog (in import settings)

Narbonne answered 2/6, 2017 at 10:26 Comment(0)
P
0

Dont style those dialogs, if u want to use material styles in pre lolipop devices,then make use of libraries

Example

Its very easy to use.

Pad answered 18/12, 2015 at 15:31 Comment(2)
can we achieve this without using this library?Mauritius
Library mentioned is open-source, u can obviously check the code and try to implement your own. Libraries are meant to save your time. For learning you can check up the codePad

© 2022 - 2024 — McMap. All rights reserved.