Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Chronometer is running

chronometer in android how to check whether chronometer is running or stop? if start then i want to stop it and if not running then start chronometer.

like image 784
Megha Avatar asked Sep 09 '11 09:09

Megha


2 Answers

You can check this using boolean variable.when you start chronometer you set boolean variable true and when it stop you set boolean variable false.

boolean isChronometerRunning = false;
if (true)  // condition on which you check whether it's start or stop
{
    chronometer.start();
    isChronometerRunning  = true;
}
else
{
  chronometer.stop();
  isChronometerRunning  = false;
}
like image 60
Megha Avatar answered Oct 05 '22 23:10

Megha


You can extend Chronomter, like this:

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;

public class MyChronometer extends Chronometer {

    private boolean isRunning = false;

    public MyChronometer(Context context) {
        super(context);
    }

    public MyChronometer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyChronometer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void start() {
        super.start();
        isRunning = true;
    }

    @Override
    public void stop() {
        super.stop();
        isRunning = false;
    }

    public boolean isRunning() {
        return isRunning;
    }

}

And then just call isRunning().

like image 20
BLuFeNiX Avatar answered Oct 06 '22 01:10

BLuFeNiX