Reputation: 895
I am having following xml file.I want to make the shape of progress bar with some background color. But my each progressBar will contain different background color. But i want to do it in only one xml file which contain shape and different color so that i can reduce xml files in drawable.
Custom_horizontal_bar:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<!--<corners android:radius="25dip" />-->
<gradient android:startColor="#ffffff" android:centerColor="#ffffff"
android:centerY="0.75" android:endColor="#ffffff" android:angle="90" />
<stroke android:width="1dp" android:color="#6B8E23" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<!--<corners android:radius="25dip" />-->
<gradient android:startColor="#4d94ff" android:endColor="#4d94ff"
android:angle="90" />
<stroke android:width="1dp" android:color="#6B8E23" />
</shape>
</clip>
</item>
</layer-list>
ProgressBar:
<ProgressBar
android:id="@+id/myProgress2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:max="100"
android:progress="60"
android:progressDrawable="@drawable/custom_horizontal_bar"
android:secondaryProgress="0" />
Kindly give any suggestion.
Upvotes: 1
Views: 904
Reputation: 17131
Try this way:
1.
LayerDrawable layerDrawable = (LayerDrawable) getResources()
.getDrawable(R.drawable.Custom_horizontal_bar);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
.findDrawableByLayerId(R.id.progress);
gradientDrawable.setColor(...);
ProgressBar myProgress2 = (ProgressBar)findViewById(R.id.myProgress2);
myProgress2.setProgressDrawable(gradientDrawable);
2.
ProgressBar myProgress2 = (ProgressBar)findViewById(R.id.myProgress2);
Drawable bgDrawable = myProgress2.getProgressDrawable();
bgDrawable.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);
myProgress2.setProgressDrawable(bgDrawable);
Upvotes: 1