b85411
b85411

Reputation: 10010

Change drawables on Android ProgressBar

I've got progress_incomplete.xml and progress_complete.xml

Both files are like this (I removed all the gradients and stroke info to save room as its not really important).

Basically the incomplete.xml has yellow colours and complete has green colours.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        ...
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
         ...
        </shape>
    </clip>
</item>
<item
    android:id="@android:id/progress"
>
    <clip>
        <shape>
          ...
        </shape>
    </clip>
</item>

</layer-list>

In my Java file, I have this:

            if (percentage == 100)
            {
                // Change colour
                progressBar.setProgressDrawable(R.drawable.progress_complete);
            }

I'm finding this isn't working though. I've also tried setBackground and setBackgroundDrawable but the change isn't taking effect.

What do I do?

Thanks

Upvotes: 1

Views: 2341

Answers (2)

dangVarmit
dangVarmit

Reputation: 5661

if (percentage == 100) {
    // Change colour
    Drawable drawable = getResources().getDrawable(R.drawable.progress_complete);
    progressBar.setProgressDrawable(drawable);
}

Upvotes: 2

Mukesh Kumar Singh
Mukesh Kumar Singh

Reputation: 4522

Refering this answer on SO, you may try this solution. It may fix you problem.

if (percentage == 100)

    {
        // Change colour
        Rect bounds = progressBar.getProgressDrawable().getBounds();
        progressBar.setProgressDrawable(R.drawable.progress_complete);
        progressBar.getProgressDrawable().setBounds(bounds);
    }

Upvotes: 0

Related Questions