[Tut] Updating UI from a Thread

Tutorials with advanced 'difficulty' and more Lines of Code.

[Tut] Updating UI from a Thread

Postby qlimax » Fri Feb 12, 2010 5:44 pm

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13. <TextView  
  14.  
  15.    android:layout_width="fill_parent"
  16.  
  17.    android:layout_height="wrap_content"
  18.  
  19.    android:text="press start to begin"
  20.  
  21.    android:id="@+id/loadingTv"
  22.  
  23.    android:inputType="textMultiLine"
  24.  
  25.    />
  26.  
  27.  
  28.  
  29. <Button
  30.  
  31. android:text="start"
  32.  
  33. android:id="@+id/startButton"
  34.  
  35. android:layout_width="wrap_content"
  36.  
  37. android:layout_height="wrap_content">
  38.  
  39. </Button>
  40.  
  41. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package ch.egsolutions.updatingui;
  3.  
  4.  
  5.  
  6. import java.util.Random;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.os.AsyncTask;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.View;
  17.  
  18. import android.view.View.OnClickListener;
  19.  
  20. import android.widget.Button;
  21.  
  22. import android.widget.TextView;
  23.  
  24.  
  25.  
  26. public class Demo extends Activity {
  27.  
  28.  
  29.  
  30.         TextView loadingTv;
  31.  
  32.  
  33.  
  34.         @Override
  35.  
  36.         public void onCreate(Bundle savedInstanceState) {
  37.  
  38.                 super.onCreate(savedInstanceState);
  39.  
  40.                 setContentView(R.layout.main);
  41.  
  42.  
  43.  
  44.                 loadingTv = (TextView) findViewById(R.id.loadingTv);
  45.  
  46.                 Button start = (Button) findViewById(R.id.startButton);
  47.  
  48.  
  49.  
  50.                 start.setOnClickListener(new OnClickListener() {
  51.  
  52.  
  53.  
  54.                         @Override
  55.  
  56.                         public void onClick(View arg0) {
  57.  
  58.                                 LoadingStuffTask task = new LoadingStuffTask();
  59.  
  60.                                 task.execute("Process started!");
  61.  
  62.                                 // the string will be passed to the doInBackground
  63.  
  64.                                 // if necessary call task.getStatus() to know the current task
  65.  
  66.                                 // status
  67.  
  68.  
  69.  
  70.                         }
  71.  
  72.                 });
  73.  
  74.  
  75.  
  76.         }
  77.  
  78.  
  79.  
  80.         private class LoadingStuffTask extends AsyncTask<String, String, String> {
  81.  
  82.                 // String,String,String are relatively the
  83.  
  84.                 // doInBackground params Type
  85.  
  86.                 // onProgressUpdate params Type
  87.  
  88.                 // onPostExecure params Type
  89.  
  90.                 //-- of course you can specify any type you need...
  91.  
  92.  
  93.  
  94.                 @Override
  95.  
  96.                 protected String doInBackground(String... params) {
  97.  
  98.                         publishProgress("\n" + params[0]);
  99.  
  100.                         // string passed to the onProgressUpdate
  101.  
  102.  
  103.  
  104.                         Random rand = new Random();
  105.  
  106.  
  107.  
  108.                         publishProgress("Loading module 1 ...");
  109.  
  110.  
  111.  
  112.                         while (rand.nextInt(1000000) != 2) {
  113.  
  114.                         }
  115.  
  116.                         // simulating
  117.  
  118.                         // extreme
  119.  
  120.                         // calculations
  121.  
  122.                         publishProgress("Loading module 2 ...");
  123.  
  124.                         while (rand.nextInt(1000000) != 1) {
  125.  
  126.                         }
  127.  
  128.  
  129.  
  130.                         return "Finish !";
  131.  
  132.                         // string passed to the onPostExecute
  133.  
  134.                 }
  135.  
  136.  
  137.  
  138.                 @Override
  139.  
  140.                 protected void onProgressUpdate(String... values) {
  141.  
  142.                         loadingTv.append(values[0] + "\n");
  143.  
  144.                 }
  145.  
  146.  
  147.  
  148.                 @Override
  149.  
  150.                 protected void onPostExecute(String result) {
  151.  
  152.                         loadingTv.append(result + "\n");
  153.  
  154.                 }
  155.  
  156.  
  157.  
  158.         }
  159.  
  160.  
  161.  
  162. }
  163.  
  164. //there is another override you can do: onPreExecute.
  165.  
  166. //It will run on the UI thread, and lets you do some ui tweaks (if needed) before
  167.  
  168. //the doInBackground is called
  169.  
  170.  
Parsed in 0.039 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 :wink:
Last edited by qlimax on Fri Feb 19, 2010 9:58 pm, edited 3 times in total.
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Postby nobb » Sun Feb 14, 2010 11:28 am

Thanks for your tutorial. That's exactly what I was trying to do.

Btw – Is there a reason why the TextView is final but the Button isn't?
nobb
Freshman
Freshman
 
Posts: 5
Joined: Thu Feb 11, 2010 4:42 pm

Re: [Tut] Updating UI from a Thread

Postby pskink » Sun Feb 14, 2010 1:05 pm

qlimax wrote:hi everyone.
First of all, although this is the advanced tutorial section, I post here because the beginner tutorial section seem to be a mix of question/answare/tutorial/off-topic, pretty much a mess.


two remarks:

1) why you use custom Thread instead of using AsyncTask? using the latter you avoid creating Handlers/Messages and so on

2) don't create Messages directly, use Message.obtain() instead, also when sending empty Messages you can use Handler.sendEmptyMessage[Delayed]() methods
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Errr ... where is the tut?

Postby erwan » Fri Feb 19, 2010 7:24 pm

Cannot find the tut, but it is just what I've been looking for. so ... where is the tut?
erwan
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 07, 2010 11:19 pm

Re: Errr ... where is the tut?

Postby qlimax » Fri Feb 19, 2010 10:00 pm

erwan wrote:Cannot find the tut, but it is just what I've been looking for. so ... where is the tut?

I've updated the post.
:)
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests