Runnable r = new Runnable(){
public void run(){
try{
For (int i = 60; i >= 1; i--) {
System.out.print(i + "\r");
// Let the thread sleep for a while.
Thread.sleep(1000);
TextView box2 = (TextView) findViewById(R.id.box2);
box2.setText("\r" + i + " sec left");
}
} catch (InterruptedException e) {
}
}
How to make number running from 60 to 0 and print on text view in android studio and what is the correct way to print in text view in android studio.thank you for your help!
Initialize your TextView in onCreate() first:
TextView box2 = (TextView) findViewById(R.id.box2);
Then you can use a handler with postDelayed() method to create a countdown timer:
final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run() {
for (int i = 60; i >= 1; i--) {
box2.setText("\r" + i + " sec left");
}
handler.postDelayed(this, 1000);
}
}, 0);