Skip to content

Most visited

Recently visited

navigation

TimePicker

public class TimePicker
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.TimePicker


A widget for selecting the time of day, in either 24-hour or AM/PM mode.

For a dialog using this view, see TimePickerDialog. See the Pickers guide for more information.

Summary

Nested classes

interface TimePicker.OnTimeChangedListener

The callback interface used to indicate the time has been adjusted. 

XML attributes

android:timePickerMode Defines the look of the widget. 

Inherited XML attributes

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

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

Public methods

void autofill(AutofillValue value)

Automatically fills the content of this view with the value.

void dispatchProvideAutofillStructure(ViewStructure structure, int flags)

Dispatches creation of a ViewStructures for autofill purposes down the hierarchy, when an Assist structure is being created as part of an autofill request.

This implementation adds in all child views of the view group, in addition to calling the default View implementation.

CharSequence getAccessibilityClassName()

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

int getAutofillType()

Describes the autofill type of this view, so an AutofillService can create the proper AutofillValue when autofilling the view.

AutofillValue getAutofillValue()

Gets the View's current autofill value.

int getBaseline()

Return the offset of the widget's text baseline from the widget's top boundary.

Integer getCurrentHour()

This method was deprecated in API level 23. Use getHour()

Integer getCurrentMinute()

This method was deprecated in API level 23. Use getMinute()

int getHour()

Returns the currently selected hour using 24-hour time.

int getMinute()

Returns the currently selected minute.

boolean is24HourView()
boolean isEnabled()

Returns the enabled status for this view.

void setCurrentHour(Integer currentHour)

This method was deprecated in API level 23. Use setHour(int)

void setCurrentMinute(Integer currentMinute)

This method was deprecated in API level 23. Use setMinute(int)

void setEnabled(boolean enabled)

Set the enabled state of this view.

void setHour(int hour)

Sets the currently selected hour using 24-hour time.

void setIs24HourView(Boolean is24HourView)

Sets whether this widget displays time in 24-hour mode or 12-hour mode with an AM/PM picker.

void setMinute(int minute)

Sets the currently selected minute.

void setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)

Set the callback that indicates the time has been adjusted by the user.

boolean validateInput()

Validates whether current input by the user is a valid time based on the locale.

Protected methods

void onRestoreInstanceState(Parcelable state)

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState().

Parcelable onSaveInstanceState()

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.

Inherited methods

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML attributes

android:timePickerMode

Defines the look of the widget. Prior to the L release, the only choice was spinner. As of L, with the Material theme selected, the default layout is clock, but this attribute can be used to force spinner to be used instead.

Must be one of the following constant values.

ConstantValueDescription
clock2Time picker with clock face to select the time.
spinner1Time picker with spinner controls to select the time.

Public constructors

TimePicker

added in API level 1
TimePicker (Context context)

Parameters
context Context

TimePicker

added in API level 1
TimePicker (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

TimePicker

added in API level 1
TimePicker (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

TimePicker

added in API level 21
TimePicker (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

autofill

added in API level 26
void autofill (AutofillValue value)

Automatically fills the content of this view with the value.

Views support the Autofill Framework mainly by:

  • Providing the metadata defining what the view means and how it can be autofilled.
  • Implementing the methods that autofill the view.

onProvideAutofillStructure(ViewStructure, int) is responsible for the former, this method is responsible for latter.

This method does nothing by default, but when overridden it typically:

  1. Checks if the provided value matches the expected type (which is defined by getAutofillType()).
  2. Checks if the view is editable - if it isn't, it should return right away.
  3. Call the proper getter method on AutofillValue to fetch the actual value.
  4. Pass the actual value to the equivalent setter in the view.

For example, a text-field view could implement the method this way:

 @Override
 public void autofill(AutofillValue value) {
   if (!value.isText() || !this.isEditable()) {
      return;
   }
   CharSequence text = value.getTextValue();
   if (text != null) {
     this.setText(text);
   }
 }
 

If the value is updated asynchronously, the next call to notifyValueChanged(View) must happen after the value was changed to the autofilled value. If not, the view will not be considered autofilled.

Note: After this method is called, the value returned by getAutofillValue() must be equal to the value passed to it, otherwise the view will not be highlighted as autofilled.

Parameters
value AutofillValue: value to be autofilled.

dispatchProvideAutofillStructure

added in API level 26
void dispatchProvideAutofillStructure (ViewStructure structure, 
                int flags)

Dispatches creation of a ViewStructures for autofill purposes down the hierarchy, when an Assist structure is being created as part of an autofill request.

The default implementation does the following:

Typically, this method should only be overridden by subclasses that provide a view hierarchy (such as ViewGroup) - other classes should override onProvideAutofillStructure(ViewStructure, int) or onProvideAutofillVirtualStructure(ViewStructure, int) instead.

When overridden, it must:

  • Either call super.dispatchProvideAutofillStructure(structure, flags) or explicitly set the AutofillId in the structure (for example, by calling structure.setAutofillId(getAutofillId())).
  • Decide how to handle the AUTOFILL_FLAG_INCLUDE_NOT_IMPORTANT_VIEWS flag - when set, all views in the structure should be considered important for autofill, regardless of what isImportantForAutofill() returns. We encourage you to respect this flag to provide a better user experience - this flag is typically used when an user explicitly requested autofill. If the flag is not set, then only views marked as important for autofill should be included in the structure - skipping non-important views optimizes the overall autofill performance.

This implementation adds in all child views of the view group, in addition to calling the default View implementation.

Parameters
structure ViewStructure: fill in with structured view data for autofill purposes.

flags int: optional flags.

getAccessibilityClassName

added in API level 23
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

getAutofillType

added in API level 26
int getAutofillType ()

Describes the autofill type of this view, so an AutofillService can create the proper AutofillValue when autofilling the view.

By default returns AUTOFILL_TYPE_NONE, but views should override it to properly support the Autofill Framework.

Returns
int either AUTOFILL_TYPE_NONE, AUTOFILL_TYPE_TEXT, AUTOFILL_TYPE_LIST, AUTOFILL_TYPE_DATE, or AUTOFILL_TYPE_TOGGLE.

getAutofillValue

added in API level 26
AutofillValue getAutofillValue ()

Gets the View's current autofill value.

By default returns null, but subclasses should override it and return an appropriate value to properly support the Autofill Framework.

Returns
AutofillValue

getBaseline

added in API level 1
int getBaseline ()

Return the offset of the widget's text baseline from the widget's top boundary. If this widget does not support baseline alignment, this method returns -1.

Returns
int the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported

getCurrentHour

added in API level 1
Integer getCurrentHour ()

This method was deprecated in API level 23.
Use getHour()

Returns
Integer the currently selected hour, in the range (0-23)

This value will never be null.

getCurrentMinute

added in API level 1
Integer getCurrentMinute ()

This method was deprecated in API level 23.
Use getMinute()

Returns
Integer the currently selected minute, in the range (0-59)

This value will never be null.

getHour

added in API level 23
int getHour ()

Returns the currently selected hour using 24-hour time.

Returns
int the currently selected hour, in the range (0-23)

See also:

getMinute

added in API level 23
int getMinute ()

Returns the currently selected minute.

Returns
int the currently selected minute, in the range (0-59)

See also:

is24HourView

added in API level 1
boolean is24HourView ()

Returns
boolean true if this widget displays time in 24-hour mode, false otherwise}

See also:

isEnabled

added in API level 1
boolean isEnabled ()

Returns the enabled status for this view. The interpretation of the enabled state varies by subclass.

Returns
boolean True if this view is enabled, false otherwise.

setCurrentHour

added in API level 1
void setCurrentHour (Integer currentHour)

This method was deprecated in API level 23.
Use setHour(int)

Sets the currently selected hour using 24-hour time.

Parameters
currentHour Integer: the hour to set, in the range (0-23)

This value must never be null.

setCurrentMinute

added in API level 1
void setCurrentMinute (Integer currentMinute)

This method was deprecated in API level 23.
Use setMinute(int)

Sets the currently selected minute.

Parameters
currentMinute Integer: the minute to set, in the range (0-59)

This value must never be null.

setEnabled

added in API level 1
void setEnabled (boolean enabled)

Set the enabled state of this view. The interpretation of the enabled state varies by subclass.

Parameters
enabled boolean: True if this view is enabled, false otherwise.

setHour

added in API level 23
void setHour (int hour)

Sets the currently selected hour using 24-hour time.

Parameters
hour int: the hour to set, in the range (0-23)

Value is 23 or less.

See also:

setIs24HourView

added in API level 1
void setIs24HourView (Boolean is24HourView)

Sets whether this widget displays time in 24-hour mode or 12-hour mode with an AM/PM picker.

Parameters
is24HourView Boolean: true to display in 24-hour mode, false for 12-hour mode with AM/PM

This value must never be null.

See also:

setMinute

added in API level 23
void setMinute (int minute)

Sets the currently selected minute.

Parameters
minute int: the minute to set, in the range (0-59)

Value is 59 or less.

See also:

setOnTimeChangedListener

added in API level 1
void setOnTimeChangedListener (TimePicker.OnTimeChangedListener onTimeChangedListener)

Set the callback that indicates the time has been adjusted by the user.

Parameters
onTimeChangedListener TimePicker.OnTimeChangedListener: the callback, should not be null.

validateInput

added in API level 26
boolean validateInput ()

Validates whether current input by the user is a valid time based on the locale. TimePicker will show an error message to the user if the time is not valid.

Returns
boolean true if the input is valid, false otherwise

Protected methods

onRestoreInstanceState

added in API level 1
void onRestoreInstanceState (Parcelable state)

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(). This function will never be called with a null state.

Parameters
state Parcelable: The frozen state that had previously been returned by onSaveInstanceState().

onSaveInstanceState

added in API level 1
Parcelable onSaveInstanceState ()

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.

Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.

Returns
Parcelable Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)