dev
dev

Reputation: 1085

How to change default ProgressDialog circle color in android

I am using ProgressDialog for showing progressbar

            ProgressDialog progressDialog = new ProgressDialog(context);
            progressDialog.setCancelable(false);
            progressDialog.setMessage(message);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.show();

And it is coming like this

enter image description here

I want to change the green color of the circle to red. is there any way?

I tried

.getIndeterminateDrawable().setColorFilter(0xFFFFFFFF, android.graphics.PorterDuff.Mode.MULTIPLY);

But not working.

Upvotes: 47

Views: 65854

Answers (10)

Umesh Sonawane
Umesh Sonawane

Reputation: 527

enter image description here

Create layout for progress dialog as named as custom_progress_dialog.xml in drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
    android:toDegrees="360">
    <shape android:shape="ring" android:innerRadiusRatio="3"
        android:thicknessRatio="8" android:useLevel="false">

        <size android:width="56dip" android:height="56dip" />

        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="@android:color/transparent"
            android:endColor="#1e9dff"
            android:angle="0"
             />

    </shape>
</rotate>

make sure that indeterminate = true and android:indeterminateDrawable="@drawable/custom_progress_background" In the activity layout where you want to use the loader

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loadingPanel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ProgressBar
        android:layout_width="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        android:layout_height="wrap_content"
        android:indeterminateDrawable="@drawable/custom_progress_dialog"
        android:indeterminate="true" />
</RelativeLayout>

Upvotes: 15

MCAmri
MCAmri

Reputation: 91

Get the resource from the android resource that being used by the ProgressDialog.

final ProgressDialog progress = new ProgressDialog(context);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
ProgressBar progressbar=(ProgressBar)progress.findViewById(android.R.id.progress);
progressbar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C60000"), android.graphics.PorterDuff.Mode.SRC_IN);

Upvotes: 7

Ramesh Prajapati
Ramesh Prajapati

Reputation: 662

Add Style.xml

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorPrimary</item>
</style>

set style to progress dialog

pdialog = new ProgressDialog(context, R.style.MyAlertDialogStyle);

Upvotes: 32

Andrew Terekhine
Andrew Terekhine

Reputation: 1521

If you already use material dialogs library (https://github.com/afollestad/material-dialogs#indeterminate-progress-dialogs) the simplest solution could be to use the progress dialog from that library. The circle will be automatically colored with the primary color:

MaterialDialog dialog = new MaterialDialog.Builder(context)
                    .content(R.string.loading)
                    .progress(true, 0)
                    .show();

Upvotes: 0

Mapsy
Mapsy

Reputation: 4262

If you'd like to define the colour ad-hoc, without needing inheritance or dedicated XML, you can use something like this:

/** Styles a ProgressDialog to use a themed Spinner. */
public void setStyledProgressDialog(final Context pContext, final ProgressDialog pProgressDialog, final int pColorResourceId) {
    // Allocate a new ProgressBar.
    final ProgressBar lProgressBar = new android.widget.ProgressBar(pContext, null, android.R.attr.progressBarStyle);
    // Configure the IndeterminateDrawable.
    lProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(pContext, pColorResourceId), android.graphics.PorterDuff.Mode.MULTIPLY);
    // Assign the ProgressBar to the ProgressDialog.
    pProgressDialog.setIndeterminateDrawable(lProgressBar.getIndeterminateDrawable());
}

Upvotes: 0

Kapil Rajput
Kapil Rajput

Reputation: 11545

In style.xml create style for dialog box :

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/black</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:background">@color/grey_dialog_box</item>
</style>

In this "android:textColorPrimary" need to define color you want to show and in java code define style of ProgressDialog like :

ProgressDialog progressDialog = new ProgressDialog(context,R.style.AppCompatAlertDialogStyle);

MORE : http://androidprogressdialogcustomcolor.blogspot.in/

Upvotes: 62

Umesh Sonawane
Umesh Sonawane

Reputation: 527

The perfect and simple solution for custom color progress bar. Custom Progress bar

public class CustomProgressBar extends ProgressBar{
    public CustomProgressBar(Context context) {
        super(context);
        this.setIndeterminate(true);
        this.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.app_blue), PorterDuff.Mode.MULTIPLY);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.app_blue), PorterDuff.Mode.MULTIPLY);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

and use it in your layout.

<RelativeLayout
        android:id="@+id/loadingPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="gone">

        <com.mayproject.views.CustomProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

Upvotes: 1

Milan Hlin&#225;k
Milan Hlin&#225;k

Reputation: 4430

This is not a perfect solution, but may help.

Also check my previous comment. https://stackoverflow.com/a/39687893/2598453

final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage(getString(R.string.progress_message));
progress.setIndeterminate(true);
progress.setCancelable(false);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    Drawable drawable = new ProgressBar(this).getIndeterminateDrawable().mutate();
    drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent),
            PorterDuff.Mode.SRC_IN);
    progress.setIndeterminateDrawable(drawable);
}

progress.show();

Upvotes: 9

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

In your Application Theme, change colorAccent

<style name="AppTheme.Bash" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">Add your color here</item>
    <item name="android:windowBackground">@color/gray</item>
</style>

use whatever color you want

Upvotes: 3

user5400869
user5400869

Reputation:

Try this:

Example where setting color to red:

ProgressBar spinner = new android.widget.ProgressBar(
                context,
                null,
                android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);

Upvotes: 4

Related Questions