Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress bar using Drawables

Tags:

android

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

like image 471
TalMihr Avatar asked Jul 29 '12 18:07

TalMihr


1 Answers

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.

like image 136
GaRRaPeTa Avatar answered Sep 21 '22 17:09

GaRRaPeTa