Change Indeterminate Progress Bar color in android programmatically

How to create & customize Indeterminate progressbar with different color style.

In this tutorial we are creating Indeterminate progress bar with multiple colors using graphics PorterDuff class. This class help us to set Indeterminate progressbar color . So here is the complete step by step tutorial for Change Indeterminate Progress Bar color in android programmatically.

android-project-download-code-button

How to Change Indeterminate Progress Bar color in android programmatically.

Code for MainActivity.java file.

 package com.setindeterminateprogressbarcolor_android_examples.com;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.graphics.PorterDuff;

public class MainActivity extends Activity {

 ProgressBar p1,p2,p3,p4;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 p1 = (ProgressBar)findViewById(R.id.progressBar1);
 p2 = (ProgressBar)findViewById(R.id.progressBar2);
 p3 = (ProgressBar)findViewById(R.id.progressBar3);
 p4 = (ProgressBar)findViewById(R.id.progressBar4);
 
 
 p1.getIndeterminateDrawable().setColorFilter(Color.parseColor("#f801fb"), PorterDuff.Mode.SRC_IN);
 
 p2.getIndeterminateDrawable().setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_IN);
 
 p3.getIndeterminateDrawable().setColorFilter(Color.parseColor("#fb0101"), PorterDuff.Mode.SRC_IN);
 
 p4.getIndeterminateDrawable().setColorFilter(Color.parseColor("#01fb30"), PorterDuff.Mode.SRC_IN);
 }

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.setindeterminateprogressbarcolor_android_examples.com.MainActivity" >

 <ProgressBar
 android:id="@+id/progressBar1"
 style="@android:style/Widget.Holo.ProgressBar.Horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:indeterminate="true"
 android:layout_marginTop="30dp" />
 
 <ProgressBar
 android:id="@+id/progressBar2"
 style="@android:style/Widget.Holo.ProgressBar.Horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:indeterminate="true"
 android:layout_marginTop="30dp"
 android:layout_below="@+id/progressBar1" />
 
 <ProgressBar
 android:id="@+id/progressBar3"
 style="@android:style/Widget.Holo.ProgressBar.Horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:indeterminate="true"
 android:layout_marginTop="30dp"
 android:layout_below="@+id/progressBar2" />
 
 <ProgressBar
 android:id="@+id/progressBar4"
 style="@android:style/Widget.Holo.ProgressBar.Horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:indeterminate="true"
 android:layout_marginTop="30dp"
 android:layout_below="@+id/progressBar3" />

</RelativeLayout>

Screenshot:

Change Indeterminate Progress Bar color in android programmaticallyClick here to download Change Indeterminate Progress Bar color in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *