Update TextView Every Second
Add following code in your onCreate() method:
Thread thread = new Thread() {
@Override
public void run() {
try {
while (!thread.isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// update TextView here!
}
});
}
} catch (InterruptedException e) {
}
}
};
thread.start();
This code starts an thread which sleeps 1000 milliseconds every round.
It's a very old question and I'm sure there are a lot of resources out there. But it's never too much to spread the word to be at the safe side. Currently, if someone else ever want to achieve what the OP asked, you can use: android.widget.TextClock
.
TextClock documentation here.
Here's what I've used:
<android.widget.TextClock
android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timeZone="GMT+0000" <!--Greenwich -->
android:format24Hour="dd MMM yyyy k:mm:ss"
android:format12Hour="@null"
android:textStyle="bold"
android:layout_alignParentEnd="true" />