-6

I want to start a timer from 0th second and for every 30 seconds,

I need to update the textview as 0.5min, 1.0min, 1.5min etc.

How do I do this?

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Samuel909
  • 13
  • 5

2 Answers2

1

Try this code,

  final Handler h = new Handler();
            h.post(new Runnable() {
                @Override
                public void run() {

                    long millis = (long) currentTime();

                    dateAndTime.setText(getDate(millis, "dd/MM/yyyy hh:mm:ss.SSS"));

                    h.postDelayed(this, 1000);
                }
            });
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

We can use an handler to handle that.

You can define as below

Handler mHandler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        // write your code update textview

        mHandler.sendEmptyMessageDelayed(0, 30000);
        return true;

    }
})

This handler will auto send and update the text again. You also need insert your code to update time for TextView.

Don't forget call the handler in the first time: mHandler.sendEmptyMessage(0);

Liem Vo
  • 5,149
  • 2
  • 18
  • 16