Skip to main content

How to change horizontal ProgressBar color 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="150dp"
        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"
        />
    <ProgressBar
        android:id="@+id/pb_red_progress"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/pb_default"
        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
        android:progressTint="#ff0600"
        />
    <TextView
        android:id="@+id/tv_red_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress color red."
        android:layout_toRightOf="@id/pb_red_progress"
        android:layout_alignBottom="@id/pb_red_progress"
        android:fontFamily="sans-serif-thin"
        android:textSize="10dp"
        android:textStyle="italic"
        />
    <ProgressBar
        android:id="@+id/pb_green_background"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/pb_red_progress"
        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
        android:progressBackgroundTint="#1a621a"
        />
    <TextView
        android:id="@+id/tv_green_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress background color green."
        android:layout_toRightOf="@id/pb_green_background"
        android:layout_alignBottom="@id/pb_green_background"
        android:fontFamily="sans-serif-thin"
        android:textSize="10dp"
        android:textStyle="italic"
        />
    <ProgressBar
        android:id="@+id/pb_colored"
        android:layout_width="225dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/pb_green_background"
        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
        android:progressTint="#4d34ff"
        android:progressBackgroundTint="#4d34ff"
        />
    <TextView
        android:id="@+id/tv_red_colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Both progress and background color changed."
        android:layout_toRightOf="@id/pb_colored"
        android:layout_alignBottom="@id/pb_colored"
        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_colored"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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_red_progress = (ProgressBar) findViewById(R.id.pb_red_progress);
        final ProgressBar pb_green_background = (ProgressBar) findViewById(R.id.pb_green_background);
        final ProgressBar pb_colored = (ProgressBar) findViewById(R.id.pb_colored);

        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_red_progress.setProgress(progressStatus);
                                    pb_green_background.setProgress(progressStatus);
                                    pb_colored.setProgress(progressStatus);
                                    // Show the progress on TextView
                                    tv.setText(progressStatus+"");
                                }
                            });
                        }
                    }
                }).start(); // Start the operation
           }
        });
    }
}




More android examples

Popular posts from this blog

Android Popup Window Example

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <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="16dp" tools:context=".MainActivity" android:background="#f5f1e0" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup Window" /> </RelativeLayout> res/layout/custom_layout.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_custom_layout" …

How to add a hint to Spinner 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="#60c4ff" > <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> spinner_item.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textStyle="italic" …

How to create a round/circle Button in Android

activity_main.xml code snippet <Button android:id ="@+id/push_button" android:layout_width="150dp" android:layout_height="150dp" android:text="Round Button" android:background="@drawable/button_bg_round" android:padding="15dp" /> res/drawable/button_bg_round.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:color="#1E90FF" android:width="5dp" /> <solid android:color="#87CEEB"/> <size android:width="150dp" android:height="150dp"/> </shape> </item> </selector> More android examples How to set Button fontHow to change Button font programmaticallyHow to set Button text style to bold and italic…