Recipe Using a Progress

This recipe demonstrates the usage of a progress bar by taking Listing 4.7 from "Recipe: Updating a Layout from a Separate Thread," which used text to show progress in a computation, and it shows the progress graphically instead. This is done by adding a progress bar object to the layout, such as:

<ProgressBar android:id="@+id/ex_progress_bar"

style="?android:attr/progressBarStyleHorizontal" android:layout_width="270px" android:layout_height="50px" android:progress="0" android:secondaryProgress="0" />

As the progress changes, the android:progress attribute can change to show a bright orange bar going across the screen.The optional android:secondaryProgress attribute shows a lighter colored orange bar that can be used to indicate a progress milestone, for example.

The activity to update the progress bar is shown in Listing 4.22. It is similar to Listing 4.7, but it uses a ProgressBar instead. Here the update results function updates the progress attribute from Java.

Listing 4.22 src/com/cookbook/handler_ui/HandlerUpdateUi.java package com.cookbook.handler_ui;

import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.ProgressBar;

public class HandlerUpdateUi extends Activity {

private static ProgressBar m_progressBar; //UI reference int percent_done = 0;

final Handler mHandler = new Handler(); // Create runnable for posting results to the UI thread final Runnable mUpdateResults = new Runnable() { public void run() {

m_progressBar.setProgress(percent_done);

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

m_progressBar = (ProgressBar) findViewById(R.id.ex_progress_bar);

Button actionButton = (Button) findViewById(R.id.action); actionButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { do_work();

//example of a computationally intensive action with UI updates private void do_work() {

Thread thread = new Thread(new Runnable() { public void run() { percent_done = 0; mHandler.post(mUpdateResults);

computation(1); percent_done = 50;

mHandler.post(mUpdateResults);

computation(2); percent_done = 100;

mHandler.post(mUpdateResults);

thread.start();

final static int SIZE=1000; //large enough to take some time double tmp;

private void computation(int val) { for(int ii=0; ii<SIZE; ii++)

tmp=val*Math.log(ii+1)/Math.log1p(jj+1);

If the updates need to be shown more often, use the postDelayed method of the handler instead of the post method and add a postDelayed to the end of the runnable update results function (similar to what was used in "Scheduling a Runnable Task from the Main Thread recipe" in Chapter 3).

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment