N
N
Nikolai Gerasimov2015-10-02 18:22:39
Java
Nikolai Gerasimov, 2015-10-02 18:22:39

How to update the time in the application?

I display time in hh:mm format in TextView. How can I make it update automatically and be equal to the system time?
Should I make a separate thread for this? Those. Every 5 seconds take the system time and, if done in a separate thread, how to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Goryachkin, 2015-10-02
@GerNik

Here's an example, if you didn't mess anything up:

Timer myTimer = new Timer(); 
final Handler uiHandler = new Handler();
final TextView txtResult = (TextView) findViewById(R.id.вашid);
myTimer.schedule(new TimerTask() { 
    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a", Locale.getDefault());
  final String strDate = simpleDateFormat.format(calendar.getTime());

        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                txtResult.setText(strDate);
            }
        });
    });
}, 5000);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question