Change progressbar color through CODE ONLY in Android

前端 未结 12 1768
北海茫月
北海茫月 2020-12-01 01:12

I have a progressBar using the ProgressBar class.

Just doing this:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizont         


        
相关标签:
12条回答
  • 2020-12-01 01:50

    Layout = activity_main.xml:

    <ProgressBar
        android:id="@+id/circle_progress_bar_middle"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:max="100"
        android:rotation="-90"
        android:indeterminate="false"
        android:progressDrawable="@drawable/my_drawable_settings2" />
    

    In Java Activity/Fragment:

    ProgressBar myProgressBar = (ProgressBar) view.findViewById(R.id.circle_progress_bar_middle);
    myProgressBar.setProgressDrawable(getResources().getDrawable(R.my_drawable_settings1));
    

    The my_drawable_settings1.xml file inside your drawable/mipmap folder:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:id="@android:id/progress">
            <shape
                android:innerRadius="55dp"
                android:shape="ring"
                android:thickness="9dp"
                android:useLevel="true">
    
                <gradient
                    android:startColor="#3594d1"
                    android:endColor="@color/white"
                    android:type="sweep" />
            </shape>
        </item>
    </layer-list>
    

    Where my_drawable_settings1 and my_drawable_settings2.xml has different colors.

    0 讨论(0)
  • 2020-12-01 01:55

    This works for me with AppCompat:

    DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);
    
    0 讨论(0)
  • 2020-12-01 02:02

    Update

    In newer versions of Android (21 works), you can change the color of a progressbar programmatically by just using setProgressTintList.

    To set it red, use:

    //bar is a ProgressBar
    bar.setProgressTintList(ColorStateList.valueOf(Color.RED));
    
    0 讨论(0)
  • 2020-12-01 02:02

    progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

    It only works above API 21

    0 讨论(0)
  • 2020-12-01 02:02

    setColorFilter with 2 arguments is deprecated and the other answer pointing to use LightingColorFilter neither worked for me so

    val progressBar = ProgressBar(context, null, android.R.attr.progressBarStyle).apply {
    
        val colorFilter = PorterDuffColorFilter(
            ContextCompat.getColor(context, R.color.yourColor),
            PorterDuff.Mode.MULTIPLY
        )
    
        indeterminateDrawable.colorFilter = colorFilter
    }
    

    That will programmatically give you the circular progress bar with your color

    0 讨论(0)
  • 2020-12-01 02:03

    This post is what you're looking for: How to change progress bar's progress color in Android

    If you want the user to choose their own colors, just make multiple-drawable XML files for each color, and select them based on the user's choice.

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