Android APIs
public class

Chronometer

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

Class Overview

Class that implements a simple timer.

You can give it a start time in the 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(). 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
Attribute Name Related Method Description
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. 
[Expand]
Inherited XML Attributes
From class android.widget.TextView
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
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()
Gets the View description.
String getFormat()
Returns the current format string as set through setFormat(String).
Chronometer.OnChronometerTickListener getOnChronometerTickListener()
void setBase(long base)
Set the time that the count-up timer is in reference to.
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 onWindowVisibilityChanged(int visibility)
Called when the window containing has change its visibility (between GONE, INVISIBLE, and VISIBLE).
[Expand]
Inherited Methods
From class android.widget.TextView
From class android.view.View
From class java.lang.Object
From interface android.view.ViewTreeObserver.OnPreDrawListener
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

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".

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

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol format.

Related Methods

Public Constructors

public Chronometer (Context context)

Added in API level 1

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

public Chronometer (Context context, AttributeSet attrs)

Added in API level 1

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

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

Added in API level 1

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

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

Added in API level 21

Public Methods

public CharSequence getAccessibilityClassName ()

Added in API level 23

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.

public long getBase ()

Added in API level 1

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

public CharSequence getContentDescription ()

Added in API level 4

Gets the View description. It briefly describes the view and is primarily used for accessibility support. Set this property to enable better accessibility support for your application. This is especially true for views that do not have textual representation (For example, ImageButton).

Returns
  • The content description.

public String getFormat ()

Added in API level 1

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

public Chronometer.OnChronometerTickListener getOnChronometerTickListener ()

Added in API level 3

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

public void setBase (long base)

Added in API level 1

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

Parameters
base Use the elapsedRealtime() time base.

public void setFormat (String format)

Added in API level 1

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 the format string.

public void setOnChronometerTickListener (Chronometer.OnChronometerTickListener listener)

Added in API level 3

Sets the listener to be called when the chronometer changes.

Parameters
listener The listener.

public void start ()

Added in API level 1

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().

public void stop ()

Added in API level 1

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

protected void onDetachedFromWindow ()

Added in API level 1

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

protected void onWindowVisibilityChanged (int visibility)

Added in API level 1

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 The new visibility of the window.