How to change horizontal progress bar color in android programmatically

How to modify horizontal progress bar theme and create with new color.

In this tutorial we are creating 5 different type of horizontal progress bars each with different color theme using getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN); method. This method helps us the choose any color from android color directory and also apply any color code. So here is the complete step by step tutorial for How to change horizontal progress bar color in android programmatically.

android-project-download-code-button

How to change horizontal progress bar color in android programmatically.

Code for MainActivity.java file.

 package com.horizontalprogressbarcolorchange_android_examples.com;

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

public class MainActivity extends Activity {

 ProgressBar progressbar1,progressbar2,progressbar3,progressbar4,progressbar5;
 int intValue = 0;
 Handler handler = new Handler();
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 progressbar1 = (ProgressBar)findViewById(R.id.progressBar1);
 progressbar2 = (ProgressBar)findViewById(R.id.progressBar2);
 progressbar3 = (ProgressBar)findViewById(R.id.progressBar3);
 progressbar4 = (ProgressBar)findViewById(R.id.progressBar4);
 progressbar5 = (ProgressBar)findViewById(R.id.progressBar5);
 
 
 // Adding colors on progress bar 
 progressbar1.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
 progressbar2.getProgressDrawable().setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_IN);
 progressbar3.getProgressDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
 progressbar4.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
 progressbar5.getProgressDrawable().setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN);
 
 new Thread(new Runnable() {
 
 @Override
 public void run() {
 // TODO Auto-generated method stub
 while(intValue < 100)
 {
 intValue++;
 
 handler.post(new Runnable() {
 
 @Override
 public void run() {
 
 
 progressbar1.setProgress(intValue);
 progressbar2.setProgress(intValue);
 progressbar3.setProgress(intValue);
 progressbar4.setProgress(intValue);
 progressbar5.setProgress(intValue);
 
 
 
 }
 });try {
 Thread.sleep(300);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 }
 }).start();
 }
 
 }

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.horizontalprogressbarcolorchange_android_examples.com.MainActivity" >

 <ProgressBar
 android:id="@+id/progressBar1"
 style="?android:attr/progressBarStyleHorizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="91dp"
 android:minHeight="60dp"
 android:minWidth="220dp"
 />

 <ProgressBar
 android:id="@+id/progressBar2"
 style="?android:attr/progressBarStyleHorizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="120dp"
 android:minHeight="60dp"
 android:minWidth="220dp"
 />
 
 <ProgressBar
 android:id="@+id/progressBar3"
 style="?android:attr/progressBarStyleHorizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="150dp"
 android:minHeight="60dp"
 android:minWidth="220dp"
 />
 <ProgressBar
 android:id="@+id/progressBar4"
 style="?android:attr/progressBarStyleHorizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="180dp"
 android:minHeight="60dp"
 android:minWidth="220dp"
 />
 <ProgressBar
 android:id="@+id/progressBar5"
 style="?android:attr/progressBarStyleHorizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="210dp"
 android:minHeight="60dp"
 android:minWidth="220dp"
 />
 
 
</RelativeLayout>

Screenshots :

progressbar-color-change

How to change horizontal progress bar color in android programmatically

Click here to download How to change horizontal progress bar color in android programmatically project with source code.

Leave a Reply

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