Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java second to countdown --
package com.example.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.arover.moment.Moment;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
TextView text;
int availableSeconds = 130; // the seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(() -> makeTime(availableSeconds--));
}
}, 0L, 1000L);
}
public void makeTime(int val) {
int minutes = val / 60;
int seconds = val % 60;
String formatted = String.format("%02d", minutes) + " : " + String.format("%02d", seconds);
text.setText(formatted);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment