I want to display current time and keep it updating, in this format example: 09:41 Hour:Minute. Is it possible to do in TextView
? I tried some ways but I'm not getting what I actually want.
Something like this should do the trick
final Handler someHandler = new Handler(getMainLooper());
someHandler.postDelayed(new Runnable() {
@Override
public void run() {
tvClock.setText(new SimpleDateFormat("HH:mm", Locale.US).format(new Date()));
someHandler.postDelayed(this, 1000);
}
}, 10);
You should keep a reference to the handler and the runnable to cancel this when the Activity
goes to pause and resume when it resumes. Make sure you remove all callbacks to handler and set it to null
in onDestroy
Android has a view for this already.
http://developer.android.com/reference/android/widget/TextClock.html
You can use it directly in XML like so
<TextClock
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
It is min api 17, so you if need to go lower than that just look into
http://developer.android.com/reference/android/widget/DigitalClock.html
Worst case scenario you can subclass textview and steal the code from the textclock source. it should be fairly straightforward
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With