How to change progress bar's progress color in Android

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

    Hit the same problem while working on modifying the look/feel of the default progress bar. Here is some more info that will hopefully help people :)

    • The name of the xml file must only contain characters: a-z0-9_. (ie. no capitals!)
    • To reference your "drawable" it is R.drawable.filename
    • To override the default look, you use myProgressBar.setProgressDrawable(...), however you need can't just refer to your custom layout as R.drawable.filename, you need to retrieve it as a Drawable:
      Resources res = getResources();
      myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename);
      
    • You need to set style before setting progress/secondary progress/max (setting it afterwards for me resulted in an 'empty' progress bar)
    0 讨论(0)
  • 2020-11-22 08:12

    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"/>
    
    0 讨论(0)
  • 2020-11-22 08:12

    The most simple way of changing the foreground and background colour of a progress bar is

    <ProgressBar
                            style="@android:style/Widget.ProgressBar.Horizontal"
                            android:id="@+id/pb_main"
                            android:layout_width="match_parent"
                            android:layout_height="8dp"
                            android:progress="30"
                            android:progressTint="#82e9de"
                            android:progressBackgroundTint="#82e9de"
                            />
    

    just add

                            android:progressTint="#82e9de" //for foreground colour
                            android:progressBackgroundTint="#82e9de" //for background colour
    
    0 讨论(0)
  • 2020-11-22 08:14

    if Indeterminate:

    ((ProgressBar)findViewById(R.id.progressBar))
        .getIndeterminateDrawable()
        .setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
    
    0 讨论(0)
  • 2020-11-22 08:15

    To change horizontal ProgressBar color (in kotlin):

    fun tintHorizontalProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            progress.progressTintList = ColorStateList.valueOf(color)
        } else{
            val layerDrawable = progress.progressDrawable as? LayerDrawable
            val progressDrawable = layerDrawable?.findDrawableByLayerId(android.R.id.progress)
            progressDrawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
        }
    }
    

    To change indeterminate ProgressBar color:

    fun tintIndeterminateProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            progress.indeterminateTintList = ColorStateList.valueOf(color)
        } else {
            (progress.indeterminateDrawable as? LayerDrawable)?.apply {
                if (numberOfLayers >= 2) {
                    setId(0, android.R.id.progress)
                    setId(1, android.R.id.secondaryProgress)
                    val progressDrawable = findDrawableByLayerId(android.R.id.progress).mutate()
                    progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
                }
            }
        }
    }
    

    And it finally normally tint pre-lollipop progressBars

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

    For my indeterminate progressbar (spinner) I just set a color filter on the drawable. Works great and just one line.

    Example where setting color to red:

    ProgressBar spinner = new android.widget.ProgressBar(
                    context,
                    null,
                    android.R.attr.progressBarStyle);
    
    spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
    

    enter image description here

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