Android Progress Dialog Gravity Issue

1.6k views Asked by At

Below is my code for custom progress bar. I have issue with just Gravity.

Here is the code.

dialog = new ProgressDialog(LandingPageActivity.this, R.style.AppBaseTheme);
            dialog.setCancelable(false);
            dialog.setIndeterminateDrawable(getResources().getDrawable(R.anim.progressbar));
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);


            dialog.show();

Style.xml

 <style name="AppBaseTheme" parent="Theme.AppCompat.Light">

        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        <item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
        <item name="android:windowBackground">@color/transparent</item>

    </style>


style name="CustomAlertDialogStyle">
        <item name="android:bottomBright">@color/transparent</item>
        <item name="android:bottomDark">@color/transparent</item>
        <item name="android:bottomMedium">@color/transparent</item>
        <item name="android:centerBright">@color/transparent</item>
        <item name="android:centerDark">@color/transparent</item>
        <item name="android:centerMedium">@color/transparent</item>
        <item name="android:fullBright">@color/transparent</item>
        <item name="android:fullDark">@color/transparent</item>
        <item name="android:topBright">@color/transparent</item>
        <item name="android:topDark">@color/transparent</item>
        <item name="android:gravity">center</item>

    </style>

animation file code

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/anim00" android:duration="150" />
    <item android:drawable="@drawable/anim01" android:duration="150" />
    <item android:drawable="@drawable/anim02" android:duration="150" />
    <item android:drawable="@drawable/anim04" android:duration="150" />

</animation-list>

I am able to show progress bar correctly but gravity is taking Left Top by default.

Appreciate your help.

Thanks

2

There are 2 answers

1
Piyush On BEST ANSWER

Solution:

You need to make one custom style in style.xml file which has a android:Theme.Holo.Dialog theme. Like,

CustomProgress

  <style name="CustomProgress" parent="android:Theme.Holo.Dialog">
    <item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
    <item name="android:textColorPrimary">#FFFFFF</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textSize">12sp</item>
</style>

and add your CustomAlertDialogStyle to your CustomProgress style instead of AppBaseTheme.

Now use your custom style for your ProgressDialog

dialog = new ProgressDialog(LandingPageActivity.this, R.style.CustomProgress);
0
ASHISH KUMAR Tiwary On

Add following code above dialog.show().Hope it may work..

                Window window = dialog.getWindow();
                WindowManager.LayoutParams wlp = window.getAttributes();

                wlp.gravity = Gravity.BOTTOM;

                window.setAttributes(wlp);
                dialog.show();