I need to create a simple progress bar using two drawable images. One is the background of course. The other I need to scale according to a percentage float. Is it possible using Java code only? I tried the setBounds() method with no avail... :(
thanks
You can perfectly and easily do that using only XML code.
First, in the layout of your activity specify the XML in where you declare the style of the progressbar, in this case @drawable/custom_progressbar:
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/custom_progressbar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/progress_bar_background" >
</ProgressBar>
Then, in @drawable/custom_progressbar declare that the progress will be expressed with a bitmap that clips according to the progress. The id is important as it is used by Android's ProgressBar widget.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
<clip>
<bitmap android:src="@drawable/progress_bar_foreground" />
</clip>
</item>
</layer-list>
Then, in the declaration of the progress bat in the layout specify the background of the progressbar.
The background should be specified in theory in the progressbar layer list with the id android:id="@android:id/background, but it didn't work for me in this case. I think Android designers thought of that to be used as a stretchable 9patch or a tiled bitmap. I wanted to do the same as you and I only got it by declaring the background as usually in the view in the layout.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With