How to change default ProgressDialog circle color in android

前端 未结 10 906
夕颜
夕颜 2020-12-24 11:01

I am using ProgressDialog for showing progressbar

            ProgressDialog progressDialog = new ProgressDialog(context);
            progress         


        
相关标签:
10条回答
  • 2020-12-24 11:33

    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();
    
    0 讨论(0)
  • 2020-12-24 11:36

    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();
    
    0 讨论(0)
  • 2020-12-24 11:37

    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);
    
    0 讨论(0)
  • 2020-12-24 11:39

    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);
    
    0 讨论(0)
  • 2020-12-24 11:41

    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());
    }
    
    0 讨论(0)
  • 2020-12-24 11:45

    The perfect and simple solution for custom color 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>
    
    0 讨论(0)
提交回复
热议问题