Change Background of Progressdialog

Change background of ProgressDialog

The comment of Aleks G (below the question) points in the right direction. The appearance of the dialog is defined by a separate style (android:alertDialogStyle). But one cannot apply the style directly to a ProgressDialog. Now, how do I get that yellow background?

Step 1: Define a theme that inherits from Theme.Dialog:

<style name="MyTheme" parent="@android:style/Theme.Dialog">
<item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
<item name="android:textColorPrimary">#000000</item>
</style>

There, you can define things like the background color for the whole window (yellow in the question), font colors etc. What's really important is the definition of android:alertDialogStyle. This style controls the appearance of the black area in the question.

Step 2: Define the CustomAlertDialogStyle:

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

This sets the black area in the question to yellow.

Step 3: Apply MyTheme to the ProgressDialog, not CustomAlertDialogStyle:

ProgressDialog dialog = new ProgressDialog(this, R.style.MyTheme);

And here's the result:

Styled ProgressDialog

The same procedure works with AlertDialog (which is the parent class of ProgressDialog).

How to change progress dialog's background color?

Step 1: Define a theme that inherits from Theme.Dialog:

<style name="MyTheme" parent="@android:style/Theme.Dialog">
<item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
<item name="android:textColorPrimary">#000000</item>
</style>

There, you can define things like the background color for the whole window (yellow in the question), font colors etc. What's really important is the definition of android:alertDialogStyle. This style controls the appearance of the black area in the question.

Step 2: Define the CustomAlertDialogStyle:

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

This sets the black area in the question to yellow.

Step 3: Apply MyTheme to the ProgressDialog, not CustomAlertDialogStyle:

ProgressDialog dialog = new ProgressDialog(this, R.style.MyTheme);

removing background from progress dialog

ProgressDialog is deprecated. You can create your custom progress dialog using alert dialog.

public static AlertDialog showProgressBar(Context context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.layout_custom_progress_bar, null);
builder.setView(view);
AlertDialog dialog = builder.create();
dialog.show();
return dialog;
}

You can then instantiate the dialog so you can manually dismiss it. Use it like so.

AlertDialog dialog = showProgressBa(this); // this will automatically show the custom progress bar
// to dismiss
dialog.dismiss();

Here is your custom layout for the progress dialog.

//layout_custom_progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:indeterminateDrawable="@drawable/progress_bar"
android:max="100"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Your progress bar drawable. You can change the color by changing the values of android:centerColor android:endColor android:startColor

// progress_bar
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="1080" >

<shape
android:innerRadius="18dp"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false" >

<size
android:height="48dp"
android:width="48dp" />

<gradient
android:centerColor="@color/colorPrimary"
android:centerY="0.5"
android:endColor="@color/colorWhite"
android:startColor="@color/colorPrimary"
android:type="sweep"
android:useLevel="false" />

</shape>
</rotate>

How to change the activity background color while a progress dialog is on

If you want to remove that light gray background color then use below code with your progress dialog:

mProgressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHI‌​ND);

And if you want to change that color, then use another layout on the top of the background but behind the Progress Dialog until dialog is open. That layout will contain whatever color you want to show there.

You can also choose alpha property for that layout which will make background look transparent.

Why the background of ProgressDialog doesn't set to the transparent?

create custom MyTheme in values\styles.xml

<style name="MyTheme" parent="android:Theme.Holo.Dialog">
<item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
<item name="android:windowBackground">@android:color/transparent</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 also add this CustomAlertDialogStyle in values\styles.xml

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

And set ProgressDialog like:

 pd = new ProgressDialog(getActivity(),R.style.MyTheme);
pd.setCancelable(false);
pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
pd.show();


Related Topics



Leave a reply



Submit