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
(not XML)?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
One more little thing, the theme solution does work if you inherit a base theme, so for app compact your theme should be:
And then set this in the progress bar theme
as per some of the suggestions, you CAN specify a shape and clipdrawable with a colour, then set it. I have this working programatically. This is how I do it..
First make sure you import the drawable library..
import android.graphics.drawable.*;
Then use the code similar to below;
For a horizontal ProgressBar, you can use a
ColorFilter
, too, like this:Note: This modifies the appearance of all progress bars in your app. To only modify one specific progress bar, do this:
If progressBar is indeterminate then use
getIndeterminateDrawable()
instead ofgetProgressDrawable()
.Since Lollipop (API 21) you can set a progress tint:
It is so simple using attr , if you are dealing with multistyle apps:
try this way:
Declare below attribute attrs.xml
Paste below code in styles.xml
use progress bar like below
Horizontal ProgressBar
usesrectangle shape
drawable for background,ClipDrawable
constructed fromrectangle shape
for progresses (primary & secondary). Tinting changes colors to some tint. If you want targeted colors for all three separately then you can useProgressBar.setProgressDrawable()
as following:Note that order of drawable layers is important while initializing
LayerDrawable
. First drawable should be for background. As per my experiment switching ids does not work. If you set padding toprogressbar
then this approach will not work. If you need padding then you can use a container for the progressbar such as aLinearLayout
.Posted to add info about PaulieG's answer, since ateiob asked me to explain something...
I can say that there is (or at least was, at the time of writing when I looked at that current version of Android source code) a bug/issue/optimisation in the
ProgressBar
code that ignores an attempt to set the progress to a value it is already at.After calling
ProgressBar.setProgressDrawable()
, your progress bar will be blank (because you changed the drawable part).This means you need to set the progress, and redraw it. But if you just set the progress to a preserved value, it will do nothing.
You must set it to 0 first, then to the "old" value again, and the bar will redraw.
So to summarise:
Below is a method I have that does this:
As far as HappyEngineer's solution, I think it was a similar workaround, to manually set the "progress" offset. In either case, the above code should work for you.