How to change the color of the Progress Bar in Android?- (I tried one way,and it isn't working)

Teja Nandamuri picture Teja Nandamuri · Jun 26, 2015 · Viewed 42.8k times · Source

I added a progress bar to my activity using the following code:

<LinearLayout
    android:id="@+id/linlaHeaderProgress"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone" >

    <ProgressBar
        android:id="@+id/pbHeaderProgress"
        android:indeterminateOnly="true"
        android:keepScreenOn="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>
</LinearLayout>

Then I call it by:

 progressbar = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
 progressbar.setVisibility(View.VISIBLE);

The progress bar is displayed and I want to change the color of it. By default the progress bar is displayed in grey color. Here is what I tried to change the color:

I created a xml file in drawables folder and named it as activityindicator.xml The contents of this xml are:

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

    <item android:id="@android:id/secondaryProgress">


        <color android:color="#f58233" />
    </item>
    <item android:id="@android:id/progress">

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

</layer-list>

And I changed the layout file as:

<LinearLayout
    android:id="@+id/linlaHeaderProgress"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:progressDrawable="@drawable/activityindicator"
    android:orientation="vertical"
    android:visibility="gone" >

    <ProgressBar
        android:id="@+id/pbHeaderProgress"
        android:indeterminateOnly="true"
        android:keepScreenOn="true"
        android:progressDrawable="@drawable/activityindicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>
</LinearLayout>

This is what I tried, but the color is not changing. Can anyone tell me what am I doing wrong?

I am using Lollipop version.

Answer

Evan Bashir picture Evan Bashir · Jun 26, 2015

If you just want to change the colour, add a colour filter to your progress bar:

pbHeaderProgress.getIndeterminateDrawable().setColorFilter(Color.RED, Mode.MULTIPLY);

The Mode parameter, refers to the PorterDuff.Mode Values - available here.