android – how to change ProgressBar color?-ThrowExceptions

Exception or error:

i want to change Horizontal ProgressBar color

i have tried this

How to change the color of an indefinite ProgressBar?

its not working still progress run in blue color

this is my code

<ProgressBar
                 android:id="@+id/mini_progress"
                 android:layout_marginLeft="10dip"
                 android:layout_marginRight="10dip"
                 style="?android:attr/progressBarStyleHorizontal"
                 android:layout_width="match_parent"
                 android:layout_height="20dip"
                 android:layout_gravity="center_horizontal"
                 android:indeterminate="false"
                 android:indeterminateBehavior="repeat"
                 android:indeterminateOnly="true"
                 android:visibility="gone"/>

how i can change the color to Pink ?

How to solve:

Try this code –

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <solid android:color="#f58233" />
            </shape>
        </clip>

        <color android:color="#f58233" />
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <solid android:color="#f58233" />
            </shape>
        </clip>

        <color android:color="#f58233" />
    </item>

</layer-list>

Progress Bar –

<ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="200dp"
            android:layout_height="3dip"
            android:progressDrawable="@drawable/progress_bar" />

Change color codes according to your need

###

create a new drawable with something similar to the following (In this case pinkprogress.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:startColor="#F5A9F2"
                android:centerColor="#F5A9F2"
                android:centerY="0.75"
                android:endColor="#F5A9F2"
                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="#33FF33"
                  android:endColor="#008000"
                  android:angle="270" />
      </shape>
    </clip>
  </item>
</layer-list>

and set to progressbar in xml file:

 android:progressDrawable="@drawable/pinkprogress"

###

I did this to get the required color :

    android:indeterminate="true"
    android:indeterminateTintMode="src_atop"
    android:indeterminateTint="@color/myColor"

Works perfectly for me.(API 21 or higher)

###

For me this worked well.

enter image description here

<ProgressBar
            android:id="@+id/verify_progress"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:layout_below="@id/verify_phone"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:indeterminate="false"
            android:progressDrawable="@drawable/progress_bar" </ProgressBar>

drawable/progress_bar –

  <?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:startColor="@color/Gray"
                    android:centerColor="@color/Gray"
                    android:endColor="@color/Gray"
                    />
            </shape>
        </item>


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

    </layer-list>

###

Since API 21 it’s a one liner:

<ProgressBar
            ...
            android:progressTint="@color/colorAccent"/>

###

Add a renderer:

public class CustomProgressBarRenderer :ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        Control.ProgressDrawable.SetColorFilter(Color.FromRgb(182, 231, 233).ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcIn);
        //Control.ProgressTintListColor.FromRgb(182, 231, 233).ToAndroid();
        Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid());


    }
}

###

this works on pre-lollipop devices.

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(getActivity(),R.color.product_status_color), PorterDuff.Mode.MULTIPLY)

Leave a Reply

Your email address will not be published. Required fields are marked *