Create an activity and do task with Thread

Description

The following code shows how to Create an activity and do task without Thread.

Example

Main layout xml file


<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<Button
    android:id="@+id/btnStartCounter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Start"
    android:onClick="startCounter" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

Main Activity Java code


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
/*from   ww  w . j  av  a  2  s.c  o  m*/
public class MainActivity extends Activity {
    TextView txtView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txtView1 = (TextView) findViewById(R.id.textView1);
    }

    public void startCounter(View view) {
        for (int i=0; i<=1000; i++) {
            txtView1.setText(String.valueOf(i));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                     Log.d("Threading", e.getLocalizedMessage());
                  }
        }
    }
}
Create an activity and do task with Thread

Note

When you run the application and click the Start button, the application is briefly frozen, and after a while you may see the message.

To solve this problem, one option is to wrap the part of the code that contains the loop using a Thread and Runnable class.

You can use the post() method of a View to create another Runnable block to be added to the message queue.

The new Runnable block created will be executed in the UI thread, so it would be safe to execute your application:


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
/*from   w w  w  .j  a  v  a  2  s .co  m*/
public class MainActivity extends Activity {
    TextView txtView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txtView1 = (TextView) findViewById(R.id.textView1);
    }

        public void startCounter(View view) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0; i<=1000; i++) {
                        final int valueOfi = i;
                        txtView1.post(new Runnable() {
                            public void run() {
                                //UI thread for updating
                                txtView1.setText(String.valueOf(valueOfi));
                            }
                        });

                        //insert a delay
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            Log.d ("Threading", e.getLocalizedMessage());
                        }
                    }
                }
            }).start();
        }
}




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView