Chronometer

public class Chronometer
extends TextView

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Chronometer


Class that implements a simple timer.

You can give it a start time in the SystemClock#elapsedRealtime timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call start().

The timer can also count downward towards the base time by setting setCountDown(boolean) to true.

By default it will display the current timer value in the form "MM:SS" or "H:MM:SS", or you can use setFormat(String) to format the timer value into an arbitrary string.

Summary

Nested classes

interface Chronometer.OnChronometerTickListener

A callback that notifies when the chronometer has incremented on its own. 

XML attributes

android:countDown Specifies whether this Chronometer counts down or counts up from the base. 
android:format Format string: if specified, the Chronometer will display this string, with the first "%s" replaced by the current timer value in "MM:SS" or "H:MM:SS" form. 

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

Chronometer(Context context)

Initialize this Chronometer object.

Chronometer(Context context, AttributeSet attrs)

Initialize with standard view layout information.

Chronometer(Context context, AttributeSet attrs, int defStyleAttr)

Initialize with standard view layout information and style.

Chronometer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

long getBase()

Return the base time as set through setBase(long).

CharSequence getContentDescription()

Returns the View's content description.

String getFormat()

Returns the current format string as set through setFormat(String).

Chronometer.OnChronometerTickListener getOnChronometerTickListener()
boolean isCountDown()
boolean isTheFinalCountDown()
void setBase(long base)

Set the time that the count-up timer is in reference to.

void setCountDown(boolean countDown)

Set this view to count down to the base instead of counting up from it.

void setFormat(String format)

Sets the format string used for display.

void setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener)

Sets the listener to be called when the chronometer changes.

void start()

Start counting up.

void stop()

Stop counting up.

Protected methods

void onDetachedFromWindow()

This is called when the view is detached from a window.

void onVisibilityChanged(View changedView, int visibility)

Called when the visibility of the view or an ancestor of the view has changed.

void onWindowVisibilityChanged(int visibility)

Called when the window containing has change its visibility (between GONE, INVISIBLE, and VISIBLE).

Inherited methods

XML attributes

android:countDown

Specifies whether this Chronometer counts down or counts up from the base. If not specified this is false and the Chronometer counts up.

May be a boolean value, such as "true" or "false".

android:format

Format string: if specified, the Chronometer will display this string, with the first "%s" replaced by the current timer value in "MM:SS" or "H:MM:SS" form. If no format string is specified, the Chronometer will simply display "MM:SS" or "H:MM:SS".

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

Public constructors

Chronometer

Added in API level 1
public Chronometer (Context context)

Initialize this Chronometer object. Sets the base to the current time.

Parameters
context Context

Chronometer

Added in API level 1
public Chronometer (Context context, 
                AttributeSet attrs)

Initialize with standard view layout information. Sets the base to the current time.

Parameters
context Context

attrs AttributeSet

Chronometer

Added in API level 1
public Chronometer (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Initialize with standard view layout information and style. Sets the base to the current time.

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Chronometer

Added in API level 1
public Chronometer (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

getAccessibilityClassName

Added in API level 23
public CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getBase

Added in API level 1
public long getBase ()

Return the base time as set through setBase(long).

Returns
long

getContentDescription

Added in API level 4
public CharSequence getContentDescription ()

Returns the View's content description.

Note: Do not override this method, as it will have no effect on the content description presented to accessibility services. You must call setContentDescription(java.lang.CharSequence) to modify the content description.

Returns
CharSequence the content description

getFormat

Added in API level 1
public String getFormat ()

Returns the current format string as set through setFormat(String).

Returns
String

getOnChronometerTickListener

Added in API level 3
public Chronometer.OnChronometerTickListener getOnChronometerTickListener ()

Returns
Chronometer.OnChronometerTickListener The listener (may be null) that is listening for chronometer change events.

isCountDown

Added in API level 24
public boolean isCountDown ()

Returns
boolean whether this view counts down

isTheFinalCountDown

Added in API level 26
public boolean isTheFinalCountDown ()

Returns
boolean whether this is the final countdown

setBase

Added in API level 1
public void setBase (long base)

Set the time that the count-up timer is in reference to.

Parameters
base long: Use the SystemClock#elapsedRealtime time base.

setCountDown

Added in API level 24
public void setCountDown (boolean countDown)

Set this view to count down to the base instead of counting up from it.

Parameters
countDown boolean: whether this view should count down

See also:

setFormat

Added in API level 1
public void setFormat (String format)

Sets the format string used for display. The Chronometer will display this string, with the first "%s" replaced by the current timer value in "MM:SS" or "H:MM:SS" form. If the format string is null, or if you never call setFormat(), the Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS" form.

Parameters
format String: the format string.

setOnChronometerTickListener

Added in API level 3
public void setOnChronometerTickListener (Chronometer.OnChronometerTickListener listener)

Sets the listener to be called when the chronometer changes.

Parameters
listener Chronometer.OnChronometerTickListener: The listener.

start

Added in API level 1
public void start ()

Start counting up. This does not affect the base as set from setBase(long), just the view display. Chronometer works by regularly scheduling messages to the handler, even when the Widget is not visible. To make sure resource leaks do not occur, the user should make sure that each start() call has a reciprocal call to stop().

stop

Added in API level 1
public void stop ()

Stop counting up. This does not affect the base as set from setBase(long), just the view display. This stops the messages to the handler, effectively releasing resources that would be held as the chronometer is running, via start().

Protected methods

onDetachedFromWindow

Added in API level 1
protected void onDetachedFromWindow ()

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
If you override this method you must call through to the superclass implementation.

onVisibilityChanged

Added in API level 8
protected void onVisibilityChanged (View changedView, 
                int visibility)

Called when the visibility of the view or an ancestor of the view has changed.

Parameters
changedView View: The view whose visibility changed. May be this or an ancestor view. This value cannot be null.

visibility int: The new visibility, one of View.VISIBLE, View.INVISIBLE or View.GONE. Value is View.VISIBLE, View.INVISIBLE, or View.GONE

onWindowVisibilityChanged

Added in API level 1
protected void onWindowVisibilityChanged (int visibility)

Called when the window containing has change its visibility (between GONE, INVISIBLE, and VISIBLE). Note that this tells you whether or not your window is being made visible to the window manager; this does not tell you whether or not your window is obscured by other windows on the screen, even if it is itself visible.

Parameters
visibility int: The new visibility of the window. Value is View.VISIBLE, View.INVISIBLE, or View.GONE