Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="press start to begin"
- android:id="@+id/loadingTv"
- android:inputType="textMultiLine"
- />
- <Button
- android:text="start"
- android:id="@+id/startButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </Button>
- </LinearLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- package ch.egsolutions.updatingui;
- import java.util.Random;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class Demo extends Activity {
- TextView loadingTv;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- loadingTv = (TextView) findViewById(R.id.loadingTv);
- Button start = (Button) findViewById(R.id.startButton);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- LoadingStuffTask task = new LoadingStuffTask();
- task.execute("Process started!");
- // the string will be passed to the doInBackground
- // if necessary call task.getStatus() to know the current task
- // status
- }
- });
- }
- private class LoadingStuffTask extends AsyncTask<String, String, String> {
- // String,String,String are relatively the
- // doInBackground params Type
- // onProgressUpdate params Type
- // onPostExecure params Type
- //-- of course you can specify any type you need...
- @Override
- protected String doInBackground(String... params) {
- publishProgress("\n" + params[0]);
- // string passed to the onProgressUpdate
- Random rand = new Random();
- publishProgress("Loading module 1 ...");
- while (rand.nextInt(1000000) != 2) {
- }
- // simulating
- // extreme
- // calculations
- publishProgress("Loading module 2 ...");
- while (rand.nextInt(1000000) != 1) {
- }
- return "Finish !";
- // string passed to the onPostExecute
- }
- @Override
- protected void onProgressUpdate(String... values) {
- loadingTv.append(values[0] + "\n");
- }
- @Override
- protected void onPostExecute(String result) {
- loadingTv.append(result + "\n");
- }
- }
- }
- //there is another override you can do: onPreExecute.
- //It will run on the UI thread, and lets you do some ui tweaks (if needed) before
- //the doInBackground is called
Parsed in 0.023 seconds, using GeSHi 1.0.8.4
btw,
to update the ui using the classic java threading method, use handlers
look here: http://developer.android.com/guide/appe ... #threading
bye
