How to change progress bar's progress color in Android

前端 未结 30 1875
情话喂你
情话喂你 2020-11-22 07:49

I\'m using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code

相关标签:
30条回答
  • 2020-11-22 08:20

    simply use:

    PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
        mode = PorterDuff.Mode.MULTIPLY;
    }
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
        progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(Color.RED));
    } else {
        Drawable progressDrawable;
        progressDrawable = (progressBar.isIndeterminate() ? progressBar.getIndeterminateDrawable() : progressBar.getProgressDrawable()).mutate();
        progressDrawable.setColorFilter(context.getResources().getColor(Color.RED), mode);
        progressBar.setProgressDrawable(progressDrawable);
    }
    
    0 讨论(0)
  • 2020-11-22 08:22

    as per some of the suggestions, you CAN specify a shape and clipdrawable with a colour, then set it. I have this working programatically. This is how I do it..

    First make sure you import the drawable library..

    import android.graphics.drawable.*;

    Then use the code similar to below;

    ProgressBar pg = (ProgressBar)row.findViewById(R.id.progress);
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
    String MyColor = "#FF00FF";
    pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    pg.setProgressDrawable(progress);   
    pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
    pg.setProgress(45);
    
    0 讨论(0)
  • 2020-11-22 08:22
    android:progressTint="#ffffff" 
    
    0 讨论(0)
  • 2020-11-22 08:22

    Use the android.support.v4.graphics.drawable.DrawableCompat:

                Drawable progressDrawable = progressBar.getIndeterminateDrawable();
                if (progressDrawable  != null) {
                    Drawable mutateDrawable = progressDrawable.mutate();
                    DrawableCompat.setTint(mutateDrawable, primaryColor);
                    progressBar.setProgressDrawable(mutateDrawable);
                }
    
    0 讨论(0)
  • 2020-11-22 08:22

    Posted to add info about PaulieG's answer, since ateiob asked me to explain something...


    I can say that there is (or at least was, at the time of writing when I looked at that current version of Android source code) a bug/issue/optimisation in the ProgressBar code that ignores an attempt to set the progress to a value it is already at.

    • i.e. if progress = 45, and you try to set it to 45, the code will do nothing, and will not redraw the progress.

    After calling ProgressBar.setProgressDrawable(), your progress bar will be blank (because you changed the drawable part).

    This means you need to set the progress, and redraw it. But if you just set the progress to a preserved value, it will do nothing.

    You must set it to 0 first, then to the "old" value again, and the bar will redraw.


    So to summarise:

    • preserve the "old" progress value
    • update the drawable / colour (makes bar blank)
    • reset the progress to 0 (otherwise next line does nothing)
    • reset the progress to the "old" value (fixes bar)
    • invalidate

    Below is a method I have that does this:

    protected void onResume()
    {
        super.onResume();
        progBar = (ProgressBar) findViewById(R.id.progress_base);
    
        int oldProgress = progBar.getProgress();
    
        // define new drawable/colour
        final float[] roundedCorners = new float[]
            { 5, 5, 5, 5, 5, 5, 5, 5 };
        ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
            roundedCorners, null, null));
        String MyColor = "#FF00FF";
        shape.getPaint().setColor(Color.parseColor(MyColor));
        ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
            ClipDrawable.HORIZONTAL);
        progBar.setProgressDrawable(clip);
    
        progBar.setBackgroundDrawable(getResources().getDrawable(
            android.R.drawable.progress_horizontal));
    
        // work around: setProgress() ignores a change to the same value
        progBar.setProgress(0);
        progBar.setProgress(oldProgress);
    
        progBar.invalidate();
    }
    

    As far as HappyEngineer's solution, I think it was a similar workaround, to manually set the "progress" offset. In either case, the above code should work for you.

    0 讨论(0)
  • 2020-11-22 08:23

    There's probably one thing that hasn't been referred to in this answer:

    If your theme is inheriting from Theme.AppCompat, ProgressBar will assume the color you defined as "colorAccent" in your theme.

    So, using..

    <item name="colorAccent">@color/custom_color</item>

    ..will tint the color of the ProgressBar automagically to the @color/custom_color .

    0 讨论(0)
提交回复
热议问题