Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text for Buttons and TextView not changing

 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that I did this correctly. But, my Buttons and TextView do not change from their original values in my layout.


Here's the layout:




Any help would be appreciated.

Thanks!
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a direct answer, but the counter method does something bad: it keeps the event handler thread from returning. The event handler thread (which calls methods lifecycle methods like onClick) always needs to return quickly. So calling Thread.sleep in it (and even within a loop) is a big no-no. Start a Thread of your own and do the work in that.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, although I see Log entries incrementing int variable i every second the Button displayButton and TextView textField will not be updated?
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...why doesn't my switch case R.id.startButton change the startButton text to "Press to Stop"?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not the job of the event handler thread to update the GUI. By not returning from it in a timely fashion, the code deprives the OS of an opportunity to update the GUI.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removing the loop makes everything else, startButton.setText("Press to Stop");, work.
Just launching Counter1() from onCreate results in a ui with no Buttons or TextView. The Log still counts though. Fascinating.

I was attempting to get something looping in the main thread (affecting the ui) and then loop something else in a separate thread that would also affect the ui. It looks like that is not possible, correct?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need to give the OS a chance to update the GUI, and that means returning from the event handler thread quickly. You can still cause the UI to be updated from code running in other threads. Start by reading http://android-developers.blogspot.de/2009/05/painless-threading.html.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Are there any more resources that you could recommend?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what, specifically? The http://www.coderanch.com/how-to/java/AndroidFaq has some good pointers.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android threads.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't get me wrong. I'm not asking you to do any work; just what might easily come to mind. So, if you have any more links that provide insight into Android threading, or books, I would appreciate it.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My preferred solution for background tasks in Android is android.os.AsyncTask.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic