Style of ProgressBar depends on device vendor. It may looks like
for HTC devices etc. The same situation for 'indeterminate' progress with infinite round spinner.
Fortunately, ProgressBar is very flexible and allow us to customize it (for example, when we want to have the same progress line for our application at any device). Let's start from most obvious - style. Default progress bar styles provided by sdk are:
- Widget.ProgressBar.Horizontal
- Widget.ProgressBar.Small
- Widget.ProgressBar.Large
- Widget.ProgressBar.Inverse
- Widget.ProgressBar.Small.Inverse
- Widget.ProgressBar.Large.Inverse
I believe the titles speaks for itself. I prefer Widget.ProgressBar.Horizontal. Further, let's define height of dialog using layout_height attribute:
<ProgressBar
android:id="@+id/progress"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="20dp"/>
Now progress bar will looks like on pictures below.
To customize progress line we need create LayerDrawable background, something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/my_bg">
<shape>
<corners android:radius="20dip"/>
<gradient android:startColor="#C0C0C0" android:centerColor="#F8F8FF"
android:centerY="0.75" android:endColor="#ffffff" android:angle="90"/>
<stroke android:width="1dp" android:color="#00aa00"/>
</shape>
</item>
<item android:id="@+id/my_progress">
<clip>
<shape>
<corners android:radius="20dip"/>
<gradient android:startColor="#CaCaC0" android:centerColor="#2828FF"
android:centerY="0.75" android:endColor="#325423" android:angle="270"/>
</shape>
</clip>
</item>
</layer-list>
Tiny clarifications: since progress line has background and progress state at least 2 layers are required. And important moment: first layer is background while second is progress. Id of layers actually can be arbitrary (as in my example), but if you don't want to clip your layers by yourself you can use system hardcoded names: background for first layer and progress for second. In this case method tileify of ProgressBar class will perform clipping internally:
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i),
(id == R.id.progress || id == R.id.secondaryProgress));
}
...
} else if (drawable instanceof BitmapDrawable) {
...
return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
ClipDrawable.HORIZONTAL) : shapeDrawable;
}
return drawable;
}
And this is how progress bar will looks like with crazy color in our LayerDrawable:
Not bad, isn't so?
build custom progress bar
ReplyDeleteThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery
ReplyDelete