Android Thread Example

And paste the following code :

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     android:orientation="vertical" >
06
07     <ProgressBar
08         android:id="@+id/progressBar1"
09         style="?android:attr/progressBarStyleHorizontal"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:indeterminate="false"
13         android:max="10"
14         android:padding="4dip" >
15     </ProgressBar>
16
17     <Button
18         android:id="@+id/button1"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:onClick="startProgress"
22         android:text="Start" />
23
24 </LinearLayout>

Now you may open the Graphical layout editor to preview the User Interface you created:

3. Code

Now we have to write the code of the application. Use the Package Explorer to navigate to the Java file of the Activity you’ve created:

The code of this tutorial is pretty much self explanatory. The interesting part is how to update the value of the Progress bar. We are going to use a Thread to run the backgroud process that updates tha value of the progress bar. Although this example works fine, it is strongly advised to use a Handler or an AsyncTask to explicity bind the backroud thread with the UI thread.

01 package com.javacodegeeks.android.androidthreadexample;
02
03 import android.app.Activity;
04 import android.os.Bundle;
05 import android.view.View;
06 import android.widget.ProgressBar;
07
08 public class MainActivity extends Activity {
09     private ProgressBar bar;
10
11     /** Called when the activity is first created. */
12
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.main);
17         bar = (ProgressBar) findViewById(R.id.progressBar1);
18
19     }
20
21     public void startProgress(View view) {
22
23         bar.setProgress(0);
24         new Thread(new Task()).start();
25     }
26
27     class Task implements Runnable {
28         @Override
29         public void run() {
30             for (int i = 0; i <= 10; i++) {
31                 final int value = i;
32                 try {
33                     Thread.sleep(1000);
34                 } catch (InterruptedException e) {
35                     e.printStackTrace();
36                 }
37                 bar.setProgress(value);
38
39             }
40         }
41
42     }
43
44 }

4. Run the application

This is the main screen of our Application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s