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
Visual indicator of progress in some operation. Displays a bar to the user
representing how far the operation has progressed; the application can change
the amount of progress (modifying the length of the bar) as it moves forward.
-->
<ProgressBar
android:id="@+id/pb"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@id/tv"
style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_below="@id/pb"
/>
</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 = (ProgressBar) findViewById(R.id.pb);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Set the progress status zero on each button click
progressStatus = 0;
/*
A Thread is a concurrent unit of execution. It has its own call stack for
methods being invoked, their arguments and local variables. Each application
has at least one thread running when it is started, the main thread,
in the main ThreadGroup. The runtime keeps its own threads
in the system thread group.
*/
// 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.setProgress(progressStatus);
// Show the progress on TextView
tv.setText(progressStatus+"");
}
});
}
}
}).start(); // Start the operation
}
});
}
}





- How to change horizontal ProgressBar color
- How to change ProgressBar color programmatically
- How to create a ProgressBar programmatically
- ProgressBar example
- How to show an indeterminate ProgressBar
- How to change indeterminate ProgressBar color
- How to create a circle ProgressBar
- How to create a vertical ProgressBar
- How to show a ProgressDialog without text
- How to create transparent background ProgressDialog