Android Progress Bar and Thread updating
11.04.2010
The progress bar is useful for telling users that your Android Application is working on a task that might take a little longer than the usual tasks.
A progress bar is quit easy to make, which I have described here is the Dialog Boxes post here and I will therefor not go into details with this.

XML layout:
There are two text fields. One to set the maximum value of the progress bar and one to set the increment value. When pushing the Start button a progress bar is displayed and in this example it will just update the progress bar every half a second (500ms) to demonstrate the result.
When the Start button is activated the application will display a progress bar – I have set a maximum value of 200 and a increment value of 14:

A progress bar is quit easy to make, which I have described here is the Dialog Boxes post here and I will therefor not go into details with this.
Simple test layout
I have created a simple test layout like this:XML layout:
<?xml version="1.0" encoding="utf-8"?> android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Maximum value"></TextView><EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100" android:id="@+id/maximum"></EditText><TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Increment by"></TextView><EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" android:id="@+id/increment"></EditText><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:id="@+id/startbtn"></Button></LinearLayout> |
Updating the progress bar
To update the progress bar in the background, while something else is running the application has to use threads. I will be setting up a thread when the Start button is clicked. The thread will then execute some code, in this case just a simple handler that will update the progress bar, and when finished the thread will terminate itself.When the Start button is activated the application will display a progress bar – I have set a maximum value of 200 and a increment value of 14:
Complete Java code
The complete java code looks like this. I have added comments in the code, so it should be self explanatory – if not just post a comment last on this page.package huuah.progressthread;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.app.ProgressDialog;public class progressthread extends Activity implements OnClickListener { ProgressDialog dialog; int increment; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button startbtn = (Button) findViewById(R.id.startbtn); startbtn.setOnClickListener(this); } public void onClick(View view) { // get the increment value from the text box EditText et = (EditText) findViewById(R.id.increment); // convert the text value to a integer increment = Integer.parseInt(et.getText().toString()); dialog = new ProgressDialog(this); dialog.setCancelable(true); dialog.setMessage("Loading..."); // set the progress to be horizontal dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // reset the bar to the default value of 0 dialog.setProgress(0); // get the maximum value EditText max = (EditText) findViewById(R.id.maximum); // convert the text value to a integer int maximum = Integer.parseInt(max.getText().toString()); // set the maximum value dialog.setMax(maximum); // display the progressbar dialog.show(); // create a thread for updating the progress bar Thread background = new Thread (new Runnable() { public void run() { try { // enter the code to be run while displaying the progressbar. // // This example is just going to increment the progress bar: // So keep running until the progress value reaches maximum value while (dialog.getProgress()<= dialog.getMax()) { // wait 500ms between each update Thread.sleep(500); // active the update handler progressHandler.sendMessage(progressHandler.obtainMessage()); } } catch (java.lang.InterruptedException e) { // if something fails do something smart } } }); // start the background thread background.start(); } // handler for the background updating Handler progressHandler = new Handler() { public void handleMessage(Message msg) { dialog.incrementProgressBy(increment); } };} |