How to change the progressbar's progress color when you have set max and min value of progressbar. i have a progressbar and try to set max value as 15 and min value as -5. i want to show progress from -5 to 0 as progress and change the color of progress as red and show green color as 0 to 15 to the progressbar.
<style name="CustomProgressBarReturnRange" parent="android:Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item name="android:minHeight">4dp</item>
<item name="android:maxHeight">4dp</item>
</style>
<ProgressBar
android:id="@ id/returnProgress"
style="@style/CustomProgressBarReturnRange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="15"
android:min="-5"
android:progress="0"
tools:progress="20" />
CodePudding user response:
Try this
Gradient drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/progress">
<item android:id="@android:id/progress">
<clip>
<shape android:shape="rectangle">
<gradient
android:centerColor="#0C8110"
android:centerX=".4"
android:endColor="#045C07"
android:startColor="#C13C3C"
android:type="linear" />
</shape>
</clip>
</item>
</layer-list>
Linear drawable
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/progress">
<item android:id="@android:id/progress">
<clip>
<layer-list>
<item android:width="80dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#ff0000" />
</shape>
</item>
<item android:start="81dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#1AFF00" />
</shape>
</item>
</layer-list>
</clip>
</item>
Your progressbar like this
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="15"
android:min="-5"
android:progress="10"
android:progressDrawable="@drawable/your_drawable"
style="?android:attr/progressBarStyleHorizontal"
android:id="@ id/mProgressBar"/>
CodePudding user response:
<style name="CustomProgressBarReturnRange" parent="android:Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@drawable/custom_return_range_progress</item>
<item name="android:minHeight">4dp</item>
<item name="android:maxHeight">4dp</item>
</style>
<ProgressBar
android:id="@ id/returnProgress"
style="@style/CustomProgressBarReturnRange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="15"
android:min="-5"
android:progress="0" />
if (progressBar.progress <= 0)
{
progressBar.progressTintList = ColorStateList.valueOf(Color.RED)
}
else if (progressBar.progress > 0)
{
progressBar.progressTintList = ColorStateList.valueOf(Color.WHITE)
}