How to change progress bar's progress color in Android

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

    THis is what i did. Worked.

    ProgressBar:

    <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:indeterminateDrawable="@drawable/progressdrawable"
               />
    

    progressdrawable.xml:
    Here use gradient to change colour as you like. And android:toDegrees="X" increse the value of X and progressbar rotate fast. Decrease and it rotate slow.Customize according to your needs.

    <?xml version="1.0" encoding="utf-8"?>
         <rotate xmlns:android="http://schemas.android.com/apk/res/android"
                android:duration="4000"
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="360" >
    
                <shape
                    android:innerRadius="20dp"
                    android:shape="ring"
                    android:thickness="4dp"
                    android:useLevel="false" >
                    <size
                        android:height="48dp"
                        android:width="48dp" />
    
                    <gradient
                        android:centerColor="#80ec7e2a"
                        android:centerY="0.5"
                        android:endColor="#ffec7e2a"
                        android:startColor="#00ec7e2a"
                        android:type="sweep"
                        android:useLevel="false" />
                </shape>
    
            </rotate>
    

    sample:

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

    Horizontal progress bar custom material style :

    To change color of background and progress of horizontal progress bar.

    <style name="MyProgressBar" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressBackgroundTint">#69f0ae</item>
        <item name="android:progressTint">#b71c1c</item>
        <item name="android:minWidth">200dp</item>
    </style>
    

    Apply it to progress bar by setting style attribute, for custom material styles and custom progress bar check http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples

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

    You can try to change your Styles, Themes, or using android:indeterminateTint="@color/yourColor" anywhere you want, but there's just one way o doing that will work on any Android SKD version:

    If you progress bar is not indeterminate, please use:

    progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );
    

    If you progress bar is indeterminate, please use:

    progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );
    

    It's sad that Android is such a mess!

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

    This solution worked for me :

    <style name="Progressbar.White" parent="AppTheme">
        <item name="colorControlActivated">@color/white</item>
    </style>
    
    <ProgressBar
        android:layout_width="@dimen/d_40"
        android:layout_height="@dimen/d_40"
        android:indeterminate="true"
        android:theme="@style/Progressbar.White"/>
    
    0 讨论(0)
  • 2020-11-22 08:31

    This works for me. It also works for lower version too. Add this to your syles.xml

    <style name="ProgressBarTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">@color/colorPrimary</item>
    </style>
    

    And use it like this in xml

    <ProgressBar
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:theme="@style/ProgressBarTheme"
       />
    
    0 讨论(0)
  • 2020-11-22 08:31

    Here is the code for changing the color of ProgressBar by programatically.

    ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
    int colorCodeDark = Color.parseColor("#F44336");
    progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
    
    0 讨论(0)
提交回复
热议问题