Why does the timer stop working when i put the postDelayed(this,1000);
inside the if statement, just under seconds++;
?
There are 3 buttons (start,stop,reset) in the layout. Press Start->running=true,press stop->running=stop,press reset->running=false seconds=0
private void runTimer() {
final TextView timeView = (TextView) findViewById(R.id.time_view);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
String time = String.format("%d:%2d:%02d", hours, minutes, secs);
timeView.setText(time);
if (running) {
seconds++;
//handler.postDelayed(this, 1000);
//doesnt work if i put it here
}
handler.postDelayed(this, 1000);
}
});
}