lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

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

Inherited constants

Inherited fields

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

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
public TimePicker (Context context)

Parameters
context Context

TimePicker

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

Parameters
context Context

attrs AttributeSet

TimePicker

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

TimePicker

added in API level 21
public 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
public 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 AutofillManager.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
public 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
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

getAutofillType

added in API level 26
public 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.

Value is AUTOFILL_TYPE_NONE, AUTOFILL_TYPE_TEXT, AUTOFILL_TYPE_TOGGLE, AUTOFILL_TYPE_LIST or AUTOFILL_TYPE_DATE.

getAutofillValue

added in API level 26
public 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
public 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
public 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
public 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
public 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
public 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
public boolean is24HourView ()

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

isEnabled

added in API level 1
public 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
public 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
public 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
public 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
public 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
public 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
public 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
public 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
public 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
protected 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
protected 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.