Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 561
Teja Nandamuri Avatar asked Jun 26 '15 15:06

Teja Nandamuri


1 Answers

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.

like image 152
Evan Bashir Avatar answered Oct 15 '22 03:10

Evan Bashir