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
This is an old question, but using theme is not mentioned here. If your default theme is using
AppCompat
, yourProgressBar
's color will becolorAccent
you have defined.Changing
colorAccent
will also change yourProgressBar
's color, but there changes also reflects at multiple places. So, if you want a different color just for a specificPregressBar
you can do that by applying theme to thatProgressBar
:Extend your default theme and override
colorAccent
And in
ProgressBar
add theandroid:theme
attribute:So it will look something like this:
So you are just changing a
colorAccent
for your particularProgressBar
.Note: Using
style
will not work. You need to useandroid:theme
only. You can find more use of theme here: https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAHTHis is what i did. Worked.
ProgressBar:
progressdrawable.xml:
Here use gradient to change colour as you like. And android:toDegrees="X" increse the value of X and progressbar rotate fast. Decrease and it rotate slow.Customize according to your needs.
sample:
Horizontal progress bar custom material style :
To change color of background and progress of horizontal progress bar.
Apply it to progress bar by setting style attribute, for custom material styles and custom progress bar check http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples
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:
Nowadays in 2016 I found some pre-Lollipop devices don't honour the
colorAccent
setting, so my final solution for all APIs is now the following:For bonus points, it doesn't use any deprecated code. Try it!
if Indeterminate: