How to change progress bar's progress color in Android

前端 未结 30 1941
情话喂你
情话喂你 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:16

    This worked for me :

    <ProgressBar
     android:indeterminateTint="#d60909"
     ... />
    
    0 讨论(0)
  • 2020-11-22 08:16

    As per muhammad-adil suggested for SDK ver 21 and above

    android:indeterminateTint="@color/orange"
    

    in XML Works for me, is easy enough.

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

    I'm sorry that it's not the answer, but what's driving the requirement setting it from code ? And .setProgressDrawable should work if it's defined correctly

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>
    
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="5dip" />
                <gradient
                    android:startColor="@color/progress_start"
                    android:endColor="@color/progress_end"
                    android:angle="270" 
                />
            </shape>
        </clip>
    </item>
    
    </layer-list>
    
    0 讨论(0)
  • 2020-11-22 08:18

    This is not programmatically but I think it could help a lot of people anyway.
    I tried a lot and the most efficient way was to add this lines to my ProgressBar in the .xml File:

                android:indeterminate="true"
                android:indeterminateTintMode="src_atop"
                android:indeterminateTint="@color/secondary"
    

    So in the end this code did it for me:

    <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="50dp"
                android:layout_marginBottom="50dp"
                android:visibility="visible"
                android:indeterminate="true"
                android:indeterminateTintMode="src_atop"
                android:indeterminateTint="@color/secondary">
    

    This solution works for API 21+

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

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

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

    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));
    

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

    For a horizontal style ProgressBar I use:

    import android.widget.ProgressBar;
    import android.graphics.drawable.GradientDrawable;
    import android.graphics.drawable.ClipDrawable;
    import android.view.Gravity;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.LayerDrawable;
    
    public void setColours(ProgressBar progressBar,
                            int bgCol1, int bgCol2, 
                            int fg1Col1, int fg1Col2, int value1,
                            int fg2Col1, int fg2Col2, int value2)
      {
        //If solid colours are required for an element, then set
        //that elements Col1 param s the same as its Col2 param
        //(eg fg1Col1 == fg1Col2).
    
        //fgGradDirection and/or bgGradDirection could be parameters
        //if you require other gradient directions eg LEFT_RIGHT.
    
        GradientDrawable.Orientation fgGradDirection
            = GradientDrawable.Orientation.TOP_BOTTOM;
        GradientDrawable.Orientation bgGradDirection
            = GradientDrawable.Orientation.TOP_BOTTOM;
    
        //Background
        GradientDrawable bgGradDrawable = new GradientDrawable(
                bgGradDirection, new int[]{bgCol1, bgCol2});
        bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
        bgGradDrawable.setCornerRadius(5);
        ClipDrawable bgclip = new ClipDrawable(
                bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     
        bgclip.setLevel(10000);
    
        //SecondaryProgress
        GradientDrawable fg2GradDrawable = new GradientDrawable(
                fgGradDirection, new int[]{fg2Col1, fg2Col2});
        fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
        fg2GradDrawable.setCornerRadius(5);
        ClipDrawable fg2clip = new ClipDrawable(
                fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        
    
        //Progress
        GradientDrawable fg1GradDrawable = new GradientDrawable(
                fgGradDirection, new int[]{fg1Col1, fg1Col2});
        fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
        fg1GradDrawable.setCornerRadius(5);
        ClipDrawable fg1clip = new ClipDrawable(
                fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        
    
        //Setup LayerDrawable and assign to progressBar
        Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
        LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);     
        progressLayerDrawable.setId(0, android.R.id.background);
        progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
        progressLayerDrawable.setId(2, android.R.id.progress);
    
        //Copy the existing ProgressDrawable bounds to the new one.
        Rect bounds = progressBar.getProgressDrawable().getBounds();
        progressBar.setProgressDrawable(progressLayerDrawable);     
        progressBar.getProgressDrawable().setBounds(bounds);
    
        // setProgress() ignores a change to the same value, so:
        if (value1 == 0)
            progressBar.setProgress(1);
        else
            progressBar.setProgress(0);
        progressBar.setProgress(value1);
    
        // setSecondaryProgress() ignores a change to the same value, so:
        if (value2 == 0)
            progressBar.setSecondaryProgress(1);
        else
            progressBar.setSecondaryProgress(0);
        progressBar.setSecondaryProgress(value2);
    
        //now force a redraw
        progressBar.invalidate();
      }
    

    An example call would be:

      setColours(myProgressBar, 
              0xff303030,   //bgCol1  grey 
              0xff909090,   //bgCol2  lighter grey 
              0xff0000FF,   //fg1Col1 blue 
              0xffFFFFFF,   //fg1Col2 white
              50,           //value1
              0xffFF0000,   //fg2Col1 red 
              0xffFFFFFF,   //fg2Col2 white
              75);          //value2
    

    If you don't need the 'secondary progress' simply set value2 to value1.

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