Devansh Yadav Devansh Yadav - 3 months ago 18
Android Question

setProgressDrawble is filling whole progress bar

I have an progress bar -

<ProgressBar
android:indeterminateOnly="false"
android:progressDrawable="@drawable/progress_anim"
android:minHeight="10dip"
android:layout_width="170dp"
android:layout_height="10dp"
android:maxHeight="10dip"
android:layout_gravity="center_vertical"
android:progress="0"
android:max="100"
android:id="@+id/scoreProgress"/>


and i want to set it's progress color programmatically . I am doing this

scoreProgress.setProgress(unikTopics.getScore());
scoreProgress.getProgressDrawable().getCurrent()
.setColorFilter(getResources().getColor(progressColor[counter])
android.graphics.PorterDuff.Mode.SRC_IN);


and this is progress_anim.xml -

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




<item android:id="@android:id/background">
<shape>
<solid android:color="#808080"/>
</shape>
</item>

<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="@color/white"/>
</shape>
</clip>
</item>

<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#ff0000"/>
</shape>
</clip>
</item>




The problem is that if I am not setting progressdrawble color programmatically , i am getting fine result with progress being shown in red color and rest part in greyish color but if i set it programmatically whole progress bar is getting flled with that color ?

Please help, Thanks in advance.

Answer

You are coloring the entire drawable (all layers). You need to grab the @android:id/progress layer from the drawable and color it specifically.

Try this:

scoreProgress.setProgress(unikTopics.getScore());
LayerDrawable layerDrawable = (LayerDrawable)scoreProgress.getProgressDrawable();
layerDrawable.findDrawableByLayerId(android.R.id.progress).setColorFilter(getResources().getColor(progressColor[counter], android.graphics.PorterDuff.Mode.SRC_IN);