How to change progress bar's progress color in

2018-12-31 09:04发布

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 (not XML)?

30条回答
听够珍惜
2楼-- · 2018-12-31 09:30

One more little thing, the theme solution does work if you inherit a base theme, so for app compact your theme should be:

<style name="AppTheme.Custom" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/custom</item>
</style>

And then set this in the progress bar theme

<ProgressBar
    android:id="@+id/progressCircle_progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:theme="@style/AppTheme.Custom"
    android:indeterminate="true"/>
查看更多
柔情千种
3楼-- · 2018-12-31 09:31

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);
查看更多
后来的你喜欢了谁
4楼-- · 2018-12-31 09:32

For a horizontal ProgressBar, you can use a ColorFilter, too, like this:

progressBar.getProgressDrawable().setColorFilter(
    Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

Red ProgressBar using color filter

Note: This modifies the appearance of all progress bars in your app. To only modify one specific progress bar, do this:

Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);

If progressBar is indeterminate then use getIndeterminateDrawable() instead of getProgressDrawable().

Since Lollipop (API 21) you can set a progress tint:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

Red ProgressBar using progress tint

查看更多
长期被迫恋爱
5楼-- · 2018-12-31 09:34

It is so simple using attr , if you are dealing with multistyle apps:

try this way:

Declare below attribute attrs.xml

 <attr name="circularProgressTheme" format="reference"></attr>

Paste below code in styles.xml

 <style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorAccent">#FF0000</item>
    </style>

    <style name="circularProgressThemeWhite">
        <item name="android:theme">@style/ProgressThemeWhite</item>
    </style>


  <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

   <item name="circularProgressTheme">@style/circularProgressThemeWhite</item>

 </style>

use progress bar like below

  <ProgressBar
        style="?attr/circularProgressTheme"
        android:id="@+id/commonProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="visible"/>
查看更多
爱死公子算了
6楼-- · 2018-12-31 09:34

Horizontal ProgressBar uses rectangle shape drawable for background, ClipDrawable constructed from rectangle shape for progresses (primary & secondary). Tinting changes colors to some tint. If you want targeted colors for all three separately then you can use ProgressBar.setProgressDrawable() as following:

    LayerDrawable progressBarDrawable = new LayerDrawable(
        new Drawable[]{
                new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                            new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),

                new ClipDrawable(
                            new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                    new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
                            Gravity.START,
                            ClipDrawable.HORIZONTAL),

                new ClipDrawable(
                        new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
                        Gravity.START,
                        ClipDrawable.HORIZONTAL)
            });

    progressBarDrawable.setId(0,android.R.id.background);
    progressBarDrawable.setId(1,android.R.id.secondaryProgress);
    progressBarDrawable.setId(2,android.R.id.progress);
    ((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);

Note that order of drawable layers is important while initializing LayerDrawable. First drawable should be for background. As per my experiment switching ids does not work. If you set padding to progressbar then this approach will not work. If you need padding then you can use a container for the progressbar such as a LinearLayout.

查看更多
大哥的爱人
7楼-- · 2018-12-31 09:35

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.

查看更多
登录 后发表回答