BufferedReader hl = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.lines)));
while(hl.ready()){
showLines.append(hl.readLine()+"\n");
showLines.invalidate();
Thread.sleep(10);
}
That is my code but it is not redrawing when I tell it to. It is supposed to redraw after every line that is added to textview, but it still only redraws at the end? Can someone please help me, I can't figure it out.
invalidate()
does not happen immediately when you call it.invalidate()
, likeappend()
and anything else involving the UI, puts a message on a message queue, that will be processed by the main application thread as soon as you let it. Since you are wasting the user's time in pointlesssleep()
calls, plus doing flash I/O, in a loop on the main application thread, the main application thread cannot process the messages on the message queue. It will process all of yourinvalidate()
andappend()
calls after your loop is over and you return control to Android from whatever callback you are in.No, it isn't.
Correct.
The simple solution is for you to get rid of the
invalidate()
and theThread.sleep(10)
and just load the entire file contents into yourTextView
in one call.The better solution is for you to read the whole file in via an
AsyncTask
, then append the text to theTextView
in one call inonPostExecute()
. If needed, use aProgressDialog
or something to keep the user entertained while this is going on.That is bacause your invalidate() is in a thread while loop and is being acummulated, so to speak, until the loop ends, and only than it draws...
I had the same problem when using Thread.sleep() within a loop. You can use a post delayed method to draw each line, which in this case is one line per second: