17

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.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109

2 Answers2

58

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.

GONeale
  • 26,302
  • 21
  • 106
  • 149
Evan Bashir
  • 5,501
  • 2
  • 25
  • 29
9

I just found a way. I don't even need a separate xml file to change the color as the progress bar is of type "indeterminate:true"

I used the following to change the color of my progress bar:

pbHeaderProgress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C0D000"), android.graphics.PorterDuff.Mode.SRC_ATOP);

You can get variety of hex color codes from here: http://www.nthelp.com/colorcodes.htm or http://www.color-hex.com/

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109