0

I spent a lot of hours researching but with no hope. What i want to do is to change the bitmap drawable (android:id="progress) of progress_bar_clip.xml programmatically.

progress_bar_clip.xml

<?xml version="1.0" encoding="utf-8"?>

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

<item android:id="@android:id/background">
    <clip
        android:clipOrientation="vertical"
        android:drawable="@drawable/body_empty"
        android:gravity="bottom" />
</item>
<item android:id="@android:id/progress">
    <clip
        android:clipOrientation="vertical"
        android:gravity="bottom" >
        <bitmap android:src="@drawable/body_empty"
        android:id="@+id/to_be_changed" >
        </bitmap>
    </clip>
</item>

</layer-list>



<ProgressBar
    android:id="@+id/progressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="180dp"
    android:layout_height="240dp"
    android:layout_marginTop="80dp"
    android:indeterminateOnly="false"
    android:max="100"
    android:progress="-500"
    android:progressDrawable="@drawable/progress_bar_clip"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    >
</ProgressBar>

code

Resources res = getResources();
LayerDrawable layerDrawable = (LayerDrawable)res.getDrawable(R.drawable.progress_bar_clip);

Drawable newDrawable = res.getDrawable(R.drawable.i_want_to_set_this);

layerDrawable.setDrawableByLayerId(R.id.to_be_changed, newDrawable);

Animation

private void wireResultAnimation(View view) {

    final ProgressBar bar = (ProgressBar) view.findViewById(R.id.progressbar);

    final Handler handler = new Handler();
    runnable = new Runnable() {

        @Override
        public void run() {

            if (bar.getProgress() != 100) {
                bar.incrementProgressBy(1);
                handler.postDelayed(runnable, 20);
            }
        }
    };


    handler.post(runnable);

}

The animation works fine however, the drawable is not changing.

thanosChatz
  • 135
  • 2
  • 11
  • possible duplicate of [State progressbar android](http://stackoverflow.com/questions/26598518/state-progressbar-android) – corsair992 Nov 12 '14 at 23:56
  • I am not able to find something that is of help, could you give me some brief guidelines please? @corsair992 – thanosChatz Nov 13 '14 at 16:01
  • If you have a static set of drawables for different progress levels, and want them be be dynamically set as the progress drawable based on the current progress, then my answer on that question describes how to achieve that. – corsair992 Nov 13 '14 at 16:11
  • I see thanks! But what Im trying to do is to depict a different drawable inside the id=progress item depending on whether its conditions are met. – thanosChatz Nov 13 '14 at 16:14
  • What is the scenario where these conditions change? What type of conditions are they? Also, nowhere in your code do you seem to be calling `setProgressDrawable()` or getting the currently progress drawable by calling `getProgressDrawable()` - how are you going to affect the `ProgressBar` without calling any method on it? – corsair992 Nov 13 '14 at 16:25
  • Thanks again for baring with my weird problem:/ I edited again the code, my animation works properly and everything is set fine, my only issue, is not being able to change the bitmap of the progress_bar_clip.xml. The scenario is some simple if – thanosChatz Nov 13 '14 at 16:29

0 Answers0