Added in API level 1

Chronometer


open class Chronometer : TextView
kotlin.Any
   ↳ 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 to format the timer value into an arbitrary string.

Summary

Nested classes
abstract

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
Public constructors
Chronometer(context: Context!)

Initialize this Chronometer object.

Chronometer(context: Context!, attrs: AttributeSet!)

Initialize with standard view layout information.

Chronometer(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

Initialize with standard view layout information and style.

Chronometer(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open CharSequence!

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

open Long

Return the base time as set through setBase.

open CharSequence!

Returns the View's content description.

open String!

Returns the current format string as set through setFormat.

open Chronometer.OnChronometerTickListener!

open Boolean

open Boolean

open Unit
setBase(base: Long)

Set the time that the count-up timer is in reference to (in the SystemClock.elapsedRealtime time base).

open Unit
setCountDown(countDown: Boolean)

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

open Unit
setFormat(format: String!)

Sets the format string used for display.

open Unit

Sets the listener to be called when the chronometer changes.

open Unit

Start counting up.

open Unit

Stop counting up.

Protected methods
open Unit

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

open Unit
onVisibilityChanged(changedView: View, visibility: Int)

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

open Unit

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

Inherited functions
Inherited properties

XML attributes

android:countDown

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

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
Chronometer(context: Context!)

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

Chronometer

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

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

Chronometer

Added in API level 1
Chronometer(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

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

Chronometer

Added in API level 21
Chronometer(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

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.

getBase

Added in API level 1
open fun getBase(): Long

Return the base time as set through setBase.

getContentDescription

Added in API level 4
open fun getContentDescription(): CharSequence!

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.

Return
CharSequence! the content description

getFormat

Added in API level 1
open fun getFormat(): String!

Returns the current format string as set through setFormat.

getOnChronometerTickListener

Added in API level 3
open fun getOnChronometerTickListener(): Chronometer.OnChronometerTickListener!
Return
Chronometer.OnChronometerTickListener! The listener (may be null) that is listening for chronometer change events.

isCountDown

Added in API level 24
open fun isCountDown(): Boolean
Return
Boolean whether this view counts down

isTheFinalCountDown

Added in API level 26
open fun isTheFinalCountDown(): Boolean
Return
Boolean whether this is the final countdown

setBase

Added in API level 1
open fun setBase(base: Long): Unit

Set the time that the count-up timer is in reference to (in the SystemClock.elapsedRealtime time base).

Parameters
base Long: Value is a non-negative timestamp in the android.os.SystemClock#elapsedRealtime() time base.

setCountDown

Added in API level 24
open fun setCountDown(countDown: Boolean): Unit

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
open fun setFormat(format: String!): Unit

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
open fun setOnChronometerTickListener(listener: Chronometer.OnChronometerTickListener!): Unit

Sets the listener to be called when the chronometer changes.

Parameters
listener Chronometer.OnChronometerTickListener!: The listener.

start

Added in API level 1
open fun start(): Unit

Start counting up. This does not affect the base as set from setBase, 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
open fun stop(): Unit

Stop counting up. This does not affect the base as set from setBase, 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 open fun onDetachedFromWindow(): Unit

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 open fun onVisibilityChanged(
    changedView: View,
    visibility: Int
): Unit

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 VISIBLE, INVISIBLE or GONE.
Value is one of the following:

onWindowVisibilityChanged

Added in API level 1
protected open fun onWindowVisibilityChanged(visibility: Int): Unit

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 one of the following: