Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I display an updating clock in a TextView

Tags:

have a clock I want to display in a TextView.

I would have thought there was a few nice functions to do this, but I couldn't find anything. I ended up implementing my own way that uses a Handler. What I'm asking though, is there a better (more efficient) way to display a real time updating clock?

private Runnable mUpdateClockTask = new Runnable() {    public void run() {        setTime();        mClockHandler.postDelayed(this, 1000);    } }; 

That's my handler, which runs every second, and then my set time and date function is below

TextView mClockView;  public void setTime() {     Calendar cal = Calendar.getInstance();     int minutes = cal.get(Calendar.MINUTE);      if (DateFormat.is24HourFormat(this)) {         int hours = cal.get(Calendar.HOUR_OF_DAY);         mClockView.setText((hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes));     }     else {         int hours = cal.get(Calendar.HOUR);         mClockView.setText(hours + ":" + (minutes < 10 ? "0" + minutes : minutes) + " " + new DateFormatSymbols().getAmPmStrings()[cal.get(Calendar.AM_PM)]);     } } 

Cheers

like image 395
Richard Rout Avatar asked Sep 09 '11 14:09

Richard Rout


People also ask

How do I use TextClock on Android?

TextClock can display the current date and/or time as a formatted string. This view honors the 24-hour format system setting. As such, it is possible and recommended to provide two different formatting patterns: one to display the date/time in 24-hour mode and one to display the date/time in 12-hour mode.

How do I update TextView?

If you have a new text to set to the TextView , just call textView. setText(newText) , where newText is the updated text. Call this method whenever newText has changed.

What are Android TextView widgets?

In android, TextView is a user interface control that is used to set and display the text to the user based on our requirements. The TextView control will act as like label control and it won't allow users to edit the text.


2 Answers

Updating the system clock is not like updating a chronometer. We talk about the time changed on the minute (system clock minute and 00 seconds).

Using a Timer is not the right way to do this. It's not only overkill, but you must resort to some tricks to make it right.

The right way to do this (ie. update a TextView showing the time as HH:mm) is to use BroadcastReceiver like this :

BroadcastReceiver _broadcastReceiver; private final SimpleDateFormat _sdfWatchTime = new SimpleDateFormat("HH:mm"); private TextView _tvTime;  @Override public void onStart() {     super.onStart();     _broadcastReceiver = new BroadcastReceiver() {             @Override             public void onReceive(Context ctx, Intent intent) {                 if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)                     _tvTime.setText(_sdfWatchTime.format(new Date()));             }         };      registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK)); }  @Override public void onStop() {     super.onStop();     if (_broadcastReceiver != null)         unregisterReceiver(_broadcastReceiver); } 

The system will send this broadcast event at the exact beginning of every minutes based on system clock. Don't forget however to initialize your TextView beforehand (to current system time) since it is likely you will pop your UI in the middle of a minute and the TextView won't be updated until the next minute happens.

like image 54
Alex Avatar answered Oct 21 '22 05:10

Alex


Check out the Chronometer and DigitalClock classes, which are extensions of the TextView class. They should automatically do what you're looking for. If you need some additional functionality, just take a look at the source code for those and make any changes you need.

UPDATE:

Since digital clock is deprecated in API 17, I would recommend to use Text Clock instead.

like image 33
Joe Krill Avatar answered Oct 21 '22 05:10

Joe Krill