How to change the color of the Progress Bar in Android?- (I tried one way,and it isn't working)
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/
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, 2022Comments
-
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 over 7 yearsthis gives flexibility to choose from colors.xml also progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(getActivity(),R.color.product_status_color), PorterDuff.Mode.MULTIPLY)
-
Nemanja Kovacevic over 7 yearsI wanted to use Color.WHITE and this line wasn't working. I had to change porter duff mode to SRC_ATOP
-
Alexander.Shtanko over 7 yearsThis method changes color of all progressbars in the app
-
Manny265 almost 7 yearsFor the mode try Mode.SRC_IN if .MULTIPLY doesnt work
-
Sam over 6 yearsis it possible do change the color in xml?