Thanks for your help. This reply is one month late as I haven't had time for further Android development learning until now. This is what I've got so far. It successfully increments from 0 to 1, however, I want to know how I can continuously call handler.postDelayed to increment all the way up to 500. Can't seem to figure it out. This is probably more of a Java question than an Android question. Any ideas?
package com.mhuang.stop; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.os.Handler; public class StopAt500 extends Activity { private Handler handler = new Handler(); public TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);; final TextView tv = new TextView(this); setContentView(tv); tv.setText("0"); count(); } public void count() { final Runnable increment = new Runnable() { public void run() { String counter = (String) tv.getText(); int i = Integer.parseInt(counter); tv.setText(Integer.toString(i+=1)); } }; handler.postDelayed(increment, 4000); } } On Jan 26, 2:08 pm, Mark Murphy <mmur...@commonsware.com> wrote: > coolbho3k wrote: > > Okay, so I am creating a test program (my version of "hello world" > > every time I start developing on a new platform) that flashes the > > numbers 0 through 500 on the screen rapidly. Here's what I have in my > > main Java file so far. Problem is, it won't display anything. Anyone > > know what is wrong? > > > I have some experience in C/C++ but I'm almost completely new to Java. > > Thanks! > > > package com.mh.test; > > > import android.app.Activity; > > import android.os.Bundle; > > import android.widget.TextView; > > > public class AndroidTest extends Activity { > > /** Called when the activity is first created. */ > > @Override > > public void onCreate(Bundle savedInstanceState) { > > TextView tv = new TextView(this); > > super.onCreate(savedInstanceState); > > for(int i=0; i >= 500; i++) > > { > > tv.setText(Integer.toString(i)); > > setContentView(tv); > > try { > > Thread.sleep(4000); > > } catch (InterruptedException e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > } > > > } > > > } > > } > > Simple answer: you're not letting Android do anything. > > Somewhat more complicated answer: > > Writing for Android is a little like writing for other UI toolkits that > have the notion of a "UI thread". Java/Swing and 16-bit Windows come to > mind from personal experience, though I assume other systems use a > similar pattern. > > All your calls to the activity (e.g., setContentView()) and all your > changes to the widgets (e.g., setText()) queue up messages in a message > loop. These messages will not get processed until you leave the callback > method you are in (onCreate()). > > What I would expect to happen if you ran this would be to get a "process > not responding" error, since this implementation seems to want to run > for 2,000 seconds (500 passes with a 4-second sleep), which is a wee bit > longer than Android wants to be tied up in one of the callbacks. > > So, here's what you do: > > In onCreate(), set up the TextView, make it be the activity's UI via > setContentView(), set the TextView to "0", and call postDelayed() on the > TextView to queue up an event 4,000ms later. In that event (a Java > Runnable object's run() method), you would increment the TextView and > call postDelayed() again to queue up another event 4,000ms later. > Lather, rinse, and repeat until you get to 500, then don't call > postDelayed() again. The most important thing, though, is after your > first call to postDelayed() is to return out of onCreate(), so Android > can start doing what you're telling it to. > > If you want it to run as fast as possible, instead of a 4-second delay, > use post() instead of postDelayed(). > > There are other approaches to this -- my book has a similar sample app, > updating a progress bar and using a Handler to manage a background thread. > > -- > Mark Murphy (a Commons Guy)http://commonsware.com > _The Busy Coder's Guide to Android Development_ Version 2.0 Published! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---