Showing posts with label custom. Show all posts
Showing posts with label custom. Show all posts

Tuesday, June 16, 2009

How to create a custom progress bar

First you need to create a progress bar layout file. Let's call it my_custom_pb.xml

<
layer-list xmlns:android="http://schemas.android.com/apk/res/android">


<item android:id="@android:id/background" android:drawable="@drawable/mybg"/>


<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80d3ff00"
android:centerColor="#80b6ff00"
android:centerY="0.75"
android:endColor="#a0cbff00"
android:angle="270"
/>

</shape>
</clip>
</item>

<item android:id="@android:id/progress">
<clip android:drawable="@drawable/myprogress"/>
</item>
</layer-list>

Notice you can either use a simple 9 patch drawable or make your own custom shape!
Then you would add that custom progress bar to the layout you want that bar to appear:

<ProgressBar
android:id="@+id/mypb"
android:layout_width="120px"
android:layout_height="10px"
android:progressDrawable="@drawable/my_custom_pb"
android:max="5"
android:progress="1" />

And finally you would access it from your code like you would with any regular progress bar:

myProgress = (ProgressBar)findViewById(R.id.mypb);
myProgress.setProgress((int)value);
--
Special Thx to Mikael for this one.