Change color ProgressBar

3

I wanted to know how to change the progress color of a circular ProgressBar (not the one that is a bar)

the code of my progress bar

  

android: id="@ + id / pg_bar"

    android:layout_width="65dp"
    android:layout_height="65dp"
    android:layout_gravity="center"
    android:layout_marginTop="25dp"
    
asked by Nicolas Schmidt 16.06.2016 в 18:25
source

3 answers

4

defines the property android:background , which in this case does not work as background of ProgressBar if not as the color of the element:

 android:background ="@drawable/mipbstyle"

inside your folder / drawable (inside / res) create the file that defines the background and contains your custom color mipbstyle.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="9" android:useLevel="false">    
        <size android:width="76dip" android:height="76dip" />
        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#001100" 
            android:endColor="#00FF00"
            android:angle="0"/>
    </shape>
</rotate> 

Modifying your progressBar a bit would be like:

<ProgressBar
    android:id="@+id/pg_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:layout_gravity="center"
    android:layout_marginTop="25dp"
    android:max="500"
    android:progress="0"
    android:progressDrawable="@drawable/mipbstyle" />

or programmatically through:

 myProgressBar.getIndeterminateDrawable()
    .setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);

in both cases you would get something similar to:

    
answered by 16.06.2016 / 18:43
source
0

Here they comment how to change it by code link I imagine that is what you ask for.

ProgressBar spinner = new android.widget.ProgressBar(
                context,
                null,
                android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
    
answered by 16.06.2016 в 18:47
0

In your xml

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/CustomProgressBar" 
    android:layout_margin="5dip" />

And in res / values / styles.xml:

<resources> 
    <style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal">
      <item name="android:indeterminateOnly">false</item>
      <item name="android:progressDrawable">@drawable/custom_progress_bar_horizontal</item>
      <item name="android:minHeight">10dip</item>
      <item name="android:maxHeight">20dip</item>
    </style>       
<style name="AppTheme" parent="android:Theme.Light" />

And custom_progress_bar_horizontal is an XML stored in the stretchable folder that defines the custom progress bar. I hope this will help you.

    
answered by 11.07.2016 в 14:26