question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ProgressIndicator] Change color of horizontal progressbar programmatically

See original GitHub issue

Is your feature request related to a problem? Please describe. I wanted to change the color of the horizontal progressindicator programmatically like this:

// binding.progressindicator references the ProgressIndicator widget in the XML layout
binding.progressindicator.indicatorColor = ResourcesCompat.getColor(resources, R.color.red, null)

But Android Studio does not resolve indicatorColor. It only resolves indicatorColors which I do not need in my case. I was confused because in the XML layout you can select indicatorColor attribute of the ProgressIndicator widget like this :

< .....ProgressIndicator 
       style="Widget.MaterialComponents.ProgressIndicator.Linear.Determinate"
       ....
      app:indicatorColor = "@color/green"    <-- that works, BUT why I can't do this programmatically ??
/>

Like changing the trackcolor via binding.progressIndicator.trackColor = ResourcesCompat.getColor(resources, R.color.yellow, null) which is possible, I would also like to change the indicator color programmatically.

I am using the version 1.3.0-alpha03 of the material design components library. What I tried to do in my project was to change the color of the indicator based on a certain treshold. So, let’s say the progress attribute value of the ProgressIndicator has reached the value 20, the indicator should be yellow, and when it has reached the value 40, it gets green and so on ( see the code below):

        binding.progressIndicator.progress = 0

        binding.progressButton.setOnClickListener {
            binding.progressIndicator.progress += 10

            if(binding.progressIndicator.progress < 25){
                // BUT : ANDROID STUDIO DOES NOT RESOLVE indicatorColor
                binding.progressIndicator.indicatorColor =ResourcesCompat.getColor(resources, R.color.red, null)
            }
            else if(binding.progressIndicator.progress < 50) {
                // BUT : ANDROID STUDIO DOES NOT RESOLVE indicatorColor
                binding.progressIndicator.trackColor = ResourcesCompat.getColor(resources, R.color.yellow, null)
            }
            else if (binding.progressIndicator.progress<75){
                // BUT : ANDROID STUDIO DOES NOT RESOLVE indicatorColor
                binding.progressIndicator.trackColor = ResourcesCompat.getColor(resources, R.color.lime, null)
            }
            else {
                // BUT : ANDROID STUDIO DOES NOT RESOLVE indicatorColor
                binding.progressIndicator.trackColor = ResourcesCompat.getColor(resources, R.color.green, null)
            }
        }

We also happily accept pull requests.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gabrielemariotticommented, Oct 8, 2020

You can use progressIndicator.setIndicatorColors(int[])

1reaction
dthomasandcommented, Feb 25, 2021

binding.progressView.setIndicatorColor(ContextCompat.getColor(context, R.color.success)) working for me in material components version 1.3.0-alpha04

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change progressbar color through CODE ONLY in Android
In newer versions of Android (21 works), you can change the color of a progressbar programmatically by just using setProgressTintList . To set...
Read more >
How to Change the ProgressBar Color in Android?
Step by Step Implementation · Go to the app > res > drawable > right-click > New > Drawable Resource File and name...
Read more >
How to set Progressbar Color Programmatically in Android Java
Changing the progress bar color in the xml can be a headache, because you will have to change values in styles and colors...
Read more >
Change progress bar style programmatically android
In this tutorial we are creating 5 different type of horizontal progress bars each with different color theme using getProgressDrawable ().setColorFilter (Color ......
Read more >
ProgressBar Tutorial With Example In Android Studio
To change to a horizontal progress bar, apply the progress bar's horizontal style. ... We can set a color or a drawable in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found