Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ProgressBar: how to set secondary color programmatically

I need to set secondary progress bar color programmatically.

I only see the method

ProgressBar.setProgressDrawable(drawable)

for set the primary color, but there isn't a method for set the secondary color.

How I can do it?

like image 561
Michele Avatar asked Jun 04 '11 09:06

Michele


3 Answers

ProgressBar.getProgressDrawable() returns a LayerDrawable in which:

LayerDrawable progressDrawable = (LayerDrawable) getProgressDrawable();
Drawable backgroundColor = progressDrawable.getDrawable(0);
Drawable secondaryColor = progressDrawable.getDrawable(1);
Drawable primaryColor = progressDrawable.getDrawable(2);

I try to modify the colors whit (example):

progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(...));

but sometimes the screen freeze, sometimes crash. Finally I abandoned the search for lack of time. Sorry :(

Michele

like image 143
Michele Avatar answered Nov 09 '22 01:11

Michele


Probably not relevant to Michele, but perhaps it will help someone else..

ProgressBar progress = (ProgressBar) fullView.findViewById(R.id.main_progressbar);
LayerDrawable progressDrawable = (LayerDrawable)progress.getProgressDrawable();
Drawable drawable = activity.getResources().getDrawable(R.drawable.WHATEVER_YOU_WANT);
ClipDrawable cd = new ClipDrawable(drawable, Gravity.LEFT,ClipDrawable.HORIZONTAL);
progressDrawable.setDrawableByLayerId(android.R.id.progress, cd);
like image 36
Amir Uval Avatar answered Nov 09 '22 01:11

Amir Uval


The drawable you specify using setProgressDrawable has background, primary and secondary drawables in it. Here is an example of ProgressBar drawable that is shipped with android:

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

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient ...  />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient .../>
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient ...  />
            </shape>
        </clip>
    </item>

</layer-list> 
like image 2
inazaruk Avatar answered Nov 09 '22 02:11

inazaruk