MainActivity.java
package com.cfsuman.androidtutorials;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
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
Button button = findViewById(R.id.button);
ProgressBar pbDefault = findViewById(R.id.pbDefault);
ProgressBar pbColored = findViewById(R.id.pbColored);
TextView tvProgress = findViewById(R.id.tvProgress);
// Change the default color of progress bar programmatically
pbColored.getProgressDrawable()
.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
// Set a click listener for Button widget
button.setOnClickListener(view -> {
// Disable the button itself
view.setEnabled(false);
// 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(100);
}catch(InterruptedException e){
e.printStackTrace();
}
// Update the progress bar
handler.post(new Runnable() {
@Override
public void run() {
pbDefault.setProgress(progressStatus);
pbColored.setProgress(progressStatus);
// Show the progress on TextView
tvProgress.setText(progressStatus+"");
}
});
}
}
}).start(); // Start the operation
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#DCDCDC"
android:padding="24dp">
<TextView
android:id="@+id/tvProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="48dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
<ProgressBar
android:id="@+id/pbDefault"
style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/tvProgress"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/tvDefault"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvProgress" />
<TextView
android:id="@+id/tvDefault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textSize="16sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="@+id/pbDefault"
app:layout_constraintEnd_toEndOf="parent"
android:text="Default Color" />
<ProgressBar
android:id="@+id/pbColored"
style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/tvProgress"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/tvColored"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pbDefault" />
<TextView
android:id="@+id/tvColored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textSize="16sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="@+id/pbColored"
app:layout_constraintEnd_toEndOf="parent"
android:text="Custom Color" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pbColored" />
</androidx.constraintlayout.widget.ConstraintLayout>