Change Progressbar Color Through Code Only in Android

How to change progress bar's progress color in Android

I'm sorry that it's not the answer, but what's driving the requirement setting it from code ?
And .setProgressDrawable should work if it's defined correctly

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

<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>

<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>

<item android:id="@android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="@color/progress_start"
android:endColor="@color/progress_end"
android:angle="270"
/>
</shape>
</clip>
</item>

</layer-list>

Change progressbar color through CODE ONLY in Android

As I found help on a topic here but can't remember the link, I'm posting my full solution which works great for my needs:

    // Draw a simple progressBar from xml
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

// Convert the color (Decimal value) to HEX value: (e.g: #4b96a0)
String color = colorDecToHex(75, 150, 160);

// Define a shape with rounded corners
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));

// Sets the progressBar color
pgDrawable.getPaint().setColor(Color.parseColor(color));

// Adds the drawable to your progressBar
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressBar.setProgressDrawable(progress);

// Sets a background to have the 3D effect
progressBar.setBackgroundDrawable(Utils.getActivity().getResources()
.getDrawable(android.R.drawable.progress_horizontal));

// Adds your progressBar to your layout
contentLayout.addView(progressBar);

And here is the code to convert DECIMAL color values to HEXADECIMAL:

public static String colorDecToHex(int p_red, int p_green, int p_blue)
{
String red = Integer.toHexString(p_red);
String green = Integer.toHexString(p_green);
String blue = Integer.toHexString(p_blue);

if (red.length() == 1)
{
red = "0" + red;
}
if (green.length() == 1)
{
green = "0" + green;
}
if (blue.length() == 1)
{
blue = "0" + blue;
}

String colorHex = "#" + red + green + blue;
return colorHex;
}

I think the last method is not that clean but it works well.

Hope this well help, too much time wasted on this progressbar.

How to change color in circular progress bar?

In the res/drawable folder, put this:

progress.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360">

<shape
android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="8"
android:useLevel="false">

<size
android:width="76dip"
android:height="76dip" />

<gradient
android:type="sweep"
android:useLevel="false"
android:startColor="#447a29"
android:endColor="#00ffffff"
android:angle="0"/>

</shape>

</rotate>

Set startColor and endColor as per your choice .

Now set that progress.xml in ProgressBar's backgound .

Like this

<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/progress"
/>

How to change ProgressBar's progress indicator color in Android

I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar:

<ProgressBar
android:id="@+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progress="50"
android:progressDrawable="@drawable/greenprogress" />

Then create a new drawable with something similar to the following (In this case greenprogress.xml):

<?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>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:startColor="#ff9d9e9d" />
</shape>
</item>

<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:endColor="#008000"
android:startColor="#33FF33" />
</shape>
</clip>
</item>

</layer-list>

You can change up the colors as needed, this will give you a green progress bar.

How to change default color of progress bar?

Please make one xml file name progress.xml and put it in res/xml folder and write the below code in that xml file.

 <?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
android:toDegrees="360">
<shape android:shape="ring" android:innerRadiusRatio="3"
android:thicknessRatio="8" android:useLevel="false">

<size android:width="76dip" android:height="76dip" />
<gradient android:type="sweep" android:useLevel="false"
android:startColor="#447a29"
android:endColor="#447a29"
android:angle="0"
/>
</shape>
</rotate>

after creating this xml file set your progressbars background as this xml ..

Like

<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@xml/progress">

Android: set color programmatically to ProgressBar

To set colors for Drawable use Drawable.setColorFilter. like:

primaryColor.setColorFilter((Color.rgb(0,128,0)), 
PorterDuff.Mode.SRC_OVER);

How to change progress bar's progress color in Android

I'm sorry that it's not the answer, but what's driving the requirement setting it from code ?
And .setProgressDrawable should work if it's defined correctly

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

<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>

<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>

<item android:id="@android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="@color/progress_start"
android:endColor="@color/progress_end"
android:angle="270"
/>
</shape>
</clip>
</item>

</layer-list>


Related Topics



Leave a reply



Submit