Change progress bar color in android programmatically

How to set ProgressBar theme style dynamically using getProgressDrawable() setColorFilter method.

Progress bar style theme can be easily changeable through some methods. So here is the complete step by step tutorial for Change progress bar color in android programmatically.

How to Change progress bar color in android programmatically.

android-project-download-code-button
Code for MainActivity.java file.

 package com.changeprogressbarcolor_android_examples.com;

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

//Import PorterDuff manually.
import android.graphics.PorterDuff;

public class MainActivity extends Activity {

 ProgressBar progressBar;
 
 int intValue = 0;
 
 Handler handler = new Handler();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 progressBar = (ProgressBar)findViewById(R.id.progressBar1);
 
 //Setting up Progress bar color Green.
 
 progressBar.getProgressDrawable().setColorFilter(Color.GREEN, 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() {
 
 
 progressBar.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.changeprogressbarcolor_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"
 />

</RelativeLayout>

Screenshot:

Change progress bar color in android programmatically

Click here to download Change progress bar color in android programmatically project.

Leave a Reply

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