Android - Get Time of Chronometer Widget

Android - Get time of chronometer widget

If you look at the source of the Chronometer class, you'll see that it doesn't store the elapsed time in a field and it calculates it internally every time it needs to update the display.

However it's relatively easy to do the same in your own code:

long elapsedMillis = SystemClock.elapsedRealtime() - chronometerInstance.getBase();

This assumes that you have started your clock something like this:

chronometerInstance.setBase(SystemClock.elapsedRealtime());
chronometerInstance.start();

Here's a full example:

public class ChronoExample extends Activity {
Chronometer mChronometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

mChronometer = new Chronometer(this);
layout.addView(mChronometer);

Button startButton = new Button(this);
startButton.setText("Start");
startButton.setOnClickListener(mStartListener);
layout.addView(startButton);

Button stopButton = new Button(this);
stopButton.setText("Stop");
stopButton.setOnClickListener(mStopListener);
layout.addView(stopButton);

Button resetButton = new Button(this);
resetButton.setText("Reset");
resetButton.setOnClickListener(mResetListener);
layout.addView(resetButton);

setContentView(layout);
}

private void showElapsedTime() {
long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis,
Toast.LENGTH_SHORT).show();
}

View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
showElapsedTime();
}
};

View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
showElapsedTime();
}
};

View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
showElapsedTime();
}
};
}

One somewhat confusing thing about Chronometer is that you can't really use it as a stopwatch that gets started, stopped and restarted again. When it's running, it will always show the time elapsed since you last reset it, no matter how many times and for how long you have stopped it in the meantime. When it is stopped, it simply stops updating the display.

If you need something like a stopwatch you'll have to subclass Chronometer or maybe create your own version using the source.

alt text

How to get elapsed time from chronometer in form of int

if you are using android.widget.Chronometer you can get the elapsed time with :

int elapsedMillis = (int) (SystemClock.elapsedRealtime() - mChronometer.getBase());

Getting the time of a Chronometer in a String?

get the elapsed time in int,

int elapsed = (int)(SystemClock.elapsedRealtime()-mChronometer.getBase());

convert it to string and then set the text in textview

textView.setText(String.valueOf(elapsed));

Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer Base?

You can use Chronometer.

You should also check this thread.

EDIT: The solution:

public class ChronoExample extends Activity {
Chronometer mChronometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

mChronometer = new Chronometer(this);

// Set the initial value
mChronometer.setText("00:10");
layout.addView(mChronometer);

Button startButton = new Button(this);
startButton.setText("Start");
startButton.setOnClickListener(mStartListener);
layout.addView(startButton);

Button stopButton = new Button(this);
stopButton.setText("Stop");
stopButton.setOnClickListener(mStopListener);
layout.addView(stopButton);

Button resetButton = new Button(this);
resetButton.setText("Reset");
resetButton.setOnClickListener(mResetListener);
layout.addView(resetButton);

setContentView(layout);
}

private void showElapsedTime() {
long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis,
Toast.LENGTH_SHORT).show();
}

View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
int stoppedMilliseconds = 0;

String chronoText = mChronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}

mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mChronometer.start();
}
};

View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
showElapsedTime();
}
};

View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
showElapsedTime();
}
};
}

Chronometer with H:MM:SS

Chronometer with H:MM:SS

Divide the time into minute , hour and second using setOnChronometerTickListener.

use this ......

Chronometer chrono  = (Chronometer) findViewById(R.id.chronomete);
chrono.setOnChronometerTickListener(new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer chronometer) {
long time = SystemClock.elapsedRealtime() - chronometer.getBase();
int h = (int)(time /3600000);
int m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
chronometer.setText(t);
}
});
chrono.setBase(SystemClock.elapsedRealtime());
chrono.setText("00:00:00");

output

EDIT

For Start

Declare globally a long variable timeWhenStopped . It is maintain the time .

private long timeWhenStopped = 0;

Start Listener... get the timeWhenStopped and start from there.

 start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chrono.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
chrono.start();
}
});

Stop Listener.... store the time in timeWhenStopped and stop.

stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timeWhenStopped = chrono.getBase() - SystemClock.elapsedRealtime();
chrono.stop();

}
});

enjoy coding............

Can Android Chronometer show 90 minutes as 90:00 NOT 1:30:00

Define instances of the controls

Chronometer chrono;
Button btnStart;
Button btnStop;
TextView txt;

And some variables

long elapsedTime=0;
String currentTime="";
long startTime=SystemClock.elapsedRealtime();
Boolean resume=false;

the Boolean flag differentiate between starting the Chronometer for the first time or resuming it after pause

Now to handle each button click event

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chrono=(Chronometer)findViewById(R.id.chrono);
btnStart=(Button)findViewById(R.id.btnStart);
btnStop=(Button)findViewById(R.id.btnStop);
btnReset=(Button)findViewById(R.id.btnReset);
txt=(TextView)findViewById(R.id.txt);

public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnStart:
btnStart.setEnabled(false);
btnStop.setEnabled(true);
if(!resume)
{
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
}
else
{

chrono.start();
}

break;
case R.id.btnStop:
btnStart.setEnabled(true);
btnStop.setEnabled(false);
chrono.stop();
chrono.setText(currentTime);
resume=true;
btnStart.setText("Resume");
break;
case R.id.btnReset:

chrono.stop();
chrono.setText("00:00");
resume=false;
btnStop.setEnabled(false);
break;
}
}

chrono.setOnChronometerTickListener(new OnChronometerTickListener()
{

public void onChronometerTick(Chronometer arg0) {
// TODO Auto-generated method stub

if(!resume)
{

long minutes=((SystemClock.elapsedRealtime()-chrono.getBase())/1000)/60;
long seconds=((SystemClock.elapsedRealtime()-chrono.getBase())/1000)%60;
currentTime=minutes+":"+seconds;
arg0.setText(currentTime);
elapsedTime=SystemClock.elapsedRealtime();
}
else
{

long minutes=((elapsedTime-chrono.getBase())/1000)/60;
long seconds=((elapsedTime-chrono.getBase())/1000)%60;
currentTime=minutes+":"+seconds;
arg0.setText(currentTime);
elapsedTime=elapsedTime+1000;
}

The onChronometerTick method handles the Chronometer tick event which occurs every second

chrono.setOnChronometerTickListener(new OnChronometerTickListener()
{

public void onChronometerTick(Chronometer arg0) {
// TODO Auto-generated method stub

if(!resume)
{

long minutes=((SystemClock.elapsedRealtime()-chrono.getBase())/1000)/60;
long seconds=((SystemClock.elapsedRealtime()-chrono.getBase())/1000)%60;
currentTime=minutes+":"+seconds;
arg0.setText(currentTime);
elapsedTime=SystemClock.elapsedRealtime();
}
else
{

long minutes=((elapsedTime-chrono.getBase())/1000)/60;
long seconds=((elapsedTime-chrono.getBase())/1000)%60;
currentTime=minutes+":"+seconds;
arg0.setText(currentTime);
elapsedTime=elapsedTime+1000;
}

How to do I retrieve the current time on a stopwatch?

I figured it out. On click you have to call the text view and set text within it.

Here is an example that works in the code provided in the question, add it in the on click method:

txtView.setText(txtTimer.getText().toString());


Related Topics



Leave a reply



Submit