[ProgressIndicator] Change color of horizontal progressbar programmatically
See original GitHub issueIs 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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
You can use
progressIndicator.setIndicatorColors(int[])
binding.progressView.setIndicatorColor(ContextCompat.getColor(context, R.color.success))
working for me in material components version1.3.0-alpha04