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

42,891

Solution 1

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.

Solution 2

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/

Share:
42,891
Teja Nandamuri
Author by

Teja Nandamuri

I am a recent graduate in iOS mobile application development. I am more enthusiastic towards iOS platform. #SOreadytohelp I would like to thank SO for helping me grow in my career.The tips provided by other developers and those who corrected me when I was wrong, really helped me to understand the iOS development to the core and I am not stopping myself to visit SO daily.I would like to do the same by contributing to SO,I would love to spend time in SO to help newbies .

Updated on July 13, 2022

Comments

  • Teja Nandamuri
    Teja Nandamuri almost 2 years

    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.

  • vikas kumar
    vikas kumar over 7 years
    this gives flexibility to choose from colors.xml also progressBar.getProgressDrawable().setColorFilter(ContextComp‌​at.getColor(getActiv‌​ity(),R.color.produc‌​t_status_color), PorterDuff.Mode.MULTIPLY)
  • Nemanja Kovacevic
    Nemanja Kovacevic over 7 years
    I wanted to use Color.WHITE and this line wasn't working. I had to change porter duff mode to SRC_ATOP
  • Alexander.Shtanko
    Alexander.Shtanko over 7 years
    This method changes color of all progressbars in the app
  • Manny265
    Manny265 almost 7 years
    For the mode try Mode.SRC_IN if .MULTIPLY doesnt work
  • Sam
    Sam over 6 years
    is it possible do change the color in xml?