Monday, August 31, 2015

How to change ProgressBar color programmatically in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#eff1f7"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        />
    <ProgressBar
        android:id="@+id/pb_default"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
        />
    <TextView
        android:id="@+id/tv_default"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default color."
        android:layout_toRightOf="@id/pb_default"
        android:layout_alignBottom="@id/pb_default"
        android:fontFamily="sans-serif-thin"
        android:textSize="10dp"
        android:textStyle="italic"
        />
    <!-- Set progress bar color programmatically -->
    <ProgressBar
        android:id="@+id/pb_custom_color"
        android:layout_width="225dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/pb_default"
        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
        />
    <TextView
        android:id="@+id/tv_red_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress bar color red."
        android:layout_toRightOf="@id/pb_custom_color"
        android:layout_alignBottom="@id/pb_custom_color"
        android:fontFamily="sans-serif-thin"
        android:textSize="10dp"
        android:textStyle="italic"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_below="@id/pb_custom_color"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ProgressBar;
import android.os.Handler;


public class MainActivity extends Activity {
    private int progressStatus = 0;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final Button btn = (Button) findViewById(R.id.btn);
        final TextView tv = (TextView) findViewById(R.id.tv);
        final ProgressBar pb_default = (ProgressBar) findViewById(R.id.pb_default);
        final ProgressBar pb_custom_color = (ProgressBar) findViewById(R.id.pb_custom_color);

        /*
            getProgressDrawable()
                Get the drawable used to draw the progress bar in progress mode.
        */

        /*
            public void setColorFilter (int color, PorterDuff.Mode mode)
                Specify a color and Porter-Duff mode to be the color filter for this drawable.
                Convenience for setColorFilter(ColorFilter) which constructs a PorterDuffColorFilter.
                Note: Setting a color filter disables tint.
        */
        // Change the default color of progress bar programmatically
        pb_custom_color.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
        //pb_custom_color.getProgressDrawable().setColorFilter(Color.parseColor("#ff0000"), PorterDuff.Mode.SRC_IN);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the progress status zero on each button click
                progressStatus = 0;
                // Start the lengthy operation in a background thread
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while(progressStatus < 100){
                            // Update the progress status
                            progressStatus +=1;

                            // Try to sleep the thread for 20 milliseconds
                            try{
                                Thread.sleep(20);
                            }catch(InterruptedException e){
                                e.printStackTrace();
                            }

                            // Update the progress bar
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    pb_default.setProgress(progressStatus);
                                    pb_custom_color.setProgress(progressStatus);
                                    // Show the progress on TextView
                                    tv.setText(progressStatus+"");
                                }
                            });
                        }
                    }
                }).start(); // Start the operation
           }
        });
    }
}



More android examples