androidloopswhile-looptextviewsettext

Setting text of TextView in android in a loop


I'm trying to set text of text view in a while loop as

t = (TextView)findViewById(R.id.textView1);  
while(true)  
{  
 t.setText(hr+":"+":"min+":"+sec);  
  try{  
      Thread.sleep(1000);  
     }  
  catch(Exception e){}  
}  

But while running the app text is not changing. When I removed the while loop then it was setting
text correctly.


Solution

  • you never change the values of hr, min and sec. Also you are calling Thread.sleep(1000); on the UI Thread, which is responsable to draw the text in your text view. Of course, it will be unable to do that while it is sleeping.