Added in API level 11

NumberPicker


open class NumberPicker : LinearLayout
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.LinearLayout
   ↳ android.widget.NumberPicker

A widget that enables the user to select a number from a predefined range. There are two flavors of this widget and which one is presented to the user depends on the current theme.

  • If the current theme is derived from android.R.style#Theme the widget presents the current value as an editable input field with an increment button above and a decrement button below. Long pressing the buttons allows for a quick change of the current value. Tapping on the input field allows to type in a desired value.
  • If the current theme is derived from android.R.style#Theme_Holo or android.R.style#Theme_Holo_Light the widget presents the current value as an editable input field with a lesser value above and a greater value below. Tapping on the lesser or greater value selects it by animating the number axis up or down to make the chosen value current. Flinging up or down allows for multiple increments or decrements of the current value. Long pressing on the lesser and greater values also allows for a quick change of the current value. Tapping on the current value allows to type in a desired value.
  • If the current theme is derived from android.R.style#Theme_Material the widget presents the current value as a scrolling vertical selector with the selected value in the center and the previous and following numbers above and below, separated by a divider. The value is changed by flinging vertically. The thickness of the divider can be changed by using the android.R.attr#selectionDividerHeight attribute and the color of the divider can be changed by using the android.R.attr#colorControlNormal attribute.

For an example of using this widget, see android.widget.TimePicker.

Summary

Nested classes
abstract

Interface used to format current value into a string for presentation.

abstract

Interface to listen for the picker scroll state.

abstract

Interface to listen for changes of the current value.

Inherited XML attributes
Inherited constants
Public constructors
NumberPicker(context: Context!)

Create a new number picker.

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

Create a new number picker.

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

Create a new number picker

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

Create a new number picker

Public methods
open Unit

open Boolean

open Boolean

open Boolean

open AccessibilityNodeProvider!

open Array<String!>!

Gets the values to be displayed instead of string values.

open Int

Returns the max value of the picker.

open Int

Returns the min value of the picker.

open Int

Retrieve the height for the divider that separates the currently selected value from the others.

open Int

open Int

open Float

open Int

Returns the value of the picker.

open Boolean

Gets whether the selector wheel wraps when reaching the min/max value.

open Unit

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

open Boolean

open Boolean

open Boolean

open Boolean

open Unit
scrollBy(x: Int, y: Int)

open Unit
setDisplayedValues(displayedValues: Array<String!>!)

Sets the values to be displayed.

open Unit
setEnabled(enabled: Boolean)

open Unit

Set the formatter to be used for formatting the current value.

open Unit
setMaxValue(maxValue: Int)

Sets the max value of the picker.

open Unit
setMinValue(minValue: Int)

Sets the min value of the picker.

open Unit

Sets the speed at which the numbers be incremented and decremented when the up and down buttons are long pressed respectively.

open Unit

Set listener to be notified for scroll state changes.

open Unit

Sets the listener to be notified on change of the current value.

open Unit

Set the height for the divider that separates the currently selected value from the others.

open Unit
setTextColor(color: Int)

Sets the text color for all the states (normal, selected, focused) to be the given color.

open Unit

Sets the text size to the given value.

open Unit
setValue(value: Int)

Set the current value for the number picker.

open Unit
setWrapSelectorWheel(wrapSelectorWheel: Boolean)

Sets whether the selector wheel shown during flinging/scrolling should wrap around the NumberPicker#getMinValue() and NumberPicker#getMaxValue() values.

Protected methods
open Int

open Int

open Int

open Boolean

open Unit

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

open Float

open Float

open Unit

open Unit
onDraw(canvas: Canvas)

open Unit
onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Inherited functions
Inherited properties

Public constructors

NumberPicker

Added in API level 11
NumberPicker(context: Context!)

Create a new number picker.

Parameters
context Context!: The application environment.

NumberPicker

Added in API level 11
NumberPicker(
    context: Context!,
    attrs: AttributeSet!)

Create a new number picker.

Parameters
context Context!: The application environment.
attrs AttributeSet!: A collection of attributes.

NumberPicker

Added in API level 11
NumberPicker(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

Create a new number picker

Parameters
context Context!: the application environment.
attrs AttributeSet!: a collection of attributes.
defStyleAttr Int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

NumberPicker

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

Create a new number picker

Parameters
context Context!: the application environment.
attrs AttributeSet!: a collection of attributes.
defStyleAttr Int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
defStyleRes Int: A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

Public methods

computeScroll

Added in API level 11
open fun computeScroll(): Unit

dispatchKeyEvent

Added in API level 11
open fun dispatchKeyEvent(event: KeyEvent!): Boolean
Parameters
event KeyEvent!: The key event to be dispatched.
Return
Boolean True if the event was handled, false otherwise.

dispatchTouchEvent

Added in API level 11
open fun dispatchTouchEvent(event: MotionEvent!): Boolean
Parameters
event MotionEvent!: The motion event to be dispatched.
Return
Boolean True if the event was handled by the view, false otherwise.

dispatchTrackballEvent

Added in API level 11
open fun dispatchTrackballEvent(event: MotionEvent!): Boolean
Parameters
event MotionEvent!: The motion event to be dispatched.
Return
Boolean True if the event was handled by the view, false otherwise.

getAccessibilityNodeProvider

Added in API level 16
open fun getAccessibilityNodeProvider(): AccessibilityNodeProvider!
Return
AccessibilityNodeProvider! The provider.

getDisplayedValues

Added in API level 11
open fun getDisplayedValues(): Array<String!>!

Gets the values to be displayed instead of string values.

Return
Array<String!>! The displayed values.

getMaxValue

Added in API level 11
open fun getMaxValue(): Int

Returns the max value of the picker.

Return
Int The max value.

getMinValue

Added in API level 11
open fun getMinValue(): Int

Returns the min value of the picker.

Return
Int The min value

getSelectionDividerHeight

Added in API level 29
open fun getSelectionDividerHeight(): Int

Retrieve the height for the divider that separates the currently selected value from the others.
The units of this value are pixels.

Return
Int The height of the divider The units of this value are pixels. {}

getSolidColor

Added in API level 11
open fun getSolidColor(): Int
Return
Int The known solid color background for this view, or 0 if the color may vary

getTextColor

Added in API level 29
open fun getTextColor(): Int
Return
Int the text color.

getTextSize

Added in API level 29
open fun getTextSize(): Float
Return
Float the size (in pixels) of the text size in this NumberPicker. Value is 0.0 or greater

getValue

Added in API level 11
open fun getValue(): Int

Returns the value of the picker.

Return
Int The value.

getWrapSelectorWheel

Added in API level 11
open fun getWrapSelectorWheel(): Boolean

Gets whether the selector wheel wraps when reaching the min/max value.

Return
Boolean True if the selector wheel wraps.

jumpDrawablesToCurrentState

Added in API level 11
open fun jumpDrawablesToCurrentState(): Unit

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

Also calls StateListAnimator#jumpToCurrentState() if there is a StateListAnimator attached to this view.
If you override this method you must call through to the superclass implementation. If you override this method you must call through to the superclass implementation.

onInterceptTouchEvent

Added in API level 11
open fun onInterceptTouchEvent(event: MotionEvent!): Boolean
Parameters
ev The motion event being dispatched down the hierarchy.
Return
Boolean Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

onTouchEvent

Added in API level 11
open fun onTouchEvent(event: MotionEvent!): Boolean
Parameters
event MotionEvent!: The motion event.
Return
Boolean True if the event was handled, false otherwise.

performClick

Added in API level 11
open fun performClick(): Boolean
Return
Boolean True there was an assigned OnClickListener that was called, false otherwise is returned.

performLongClick

Added in API level 11
open fun performLongClick(): Boolean
Return
Boolean true if one of the above receivers consumed the event, false otherwise

scrollBy

Added in API level 11
open fun scrollBy(
    x: Int,
    y: Int
): Unit
Parameters
x Int: the amount of pixels to scroll by horizontally
y Int: the amount of pixels to scroll by vertically

setDisplayedValues

Added in API level 11
open fun setDisplayedValues(displayedValues: Array<String!>!): Unit

Sets the values to be displayed.

Parameters
displayedValues Array<String!>!: The displayed values. Note: The length of the displayed values array must be equal to the range of selectable numbers which is equal to getMaxValue() - getMinValue() + 1.

setEnabled

Added in API level 11
open fun setEnabled(enabled: Boolean): Unit
Parameters
enabled Boolean: True if this view is enabled, false otherwise.

setFormatter

Added in API level 11
open fun setFormatter(formatter: NumberPicker.Formatter!): Unit

Set the formatter to be used for formatting the current value.

Note: If you have provided alternative values for the values this formatter is never invoked.

Parameters
formatter NumberPicker.Formatter!: The formatter object. If formatter is null, String#valueOf(int) will be used.

setMaxValue

Added in API level 11
open fun setMaxValue(maxValue: Int): Unit

Sets the max value of the picker.

Parameters
maxValue Int: The max value inclusive. Note: The length of the displayed values array set via setDisplayedValues(java.lang.String[]) must be equal to the range of selectable numbers which is equal to getMaxValue() - getMinValue() + 1.

setMinValue

Added in API level 11
open fun setMinValue(minValue: Int): Unit

Sets the min value of the picker.

Parameters
minValue Int: The min value inclusive. Note: The length of the displayed values array set via setDisplayedValues(java.lang.String[]) must be equal to the range of selectable numbers which is equal to getMaxValue() - getMinValue() + 1.

setOnLongPressUpdateInterval

Added in API level 11
open fun setOnLongPressUpdateInterval(intervalMillis: Long): Unit

Sets the speed at which the numbers be incremented and decremented when the up and down buttons are long pressed respectively.

The default value is 300 ms.

Parameters
intervalMillis Long: The speed (in milliseconds) at which the numbers will be incremented and decremented.

setOnScrollListener

Added in API level 11
open fun setOnScrollListener(onScrollListener: NumberPicker.OnScrollListener!): Unit

Set listener to be notified for scroll state changes.

Parameters
onScrollListener NumberPicker.OnScrollListener!: The listener.

setOnValueChangedListener

Added in API level 11
open fun setOnValueChangedListener(onValueChangedListener: NumberPicker.OnValueChangeListener!): Unit

Sets the listener to be notified on change of the current value.

Parameters
onValueChangedListener NumberPicker.OnValueChangeListener!: The listener.

setSelectionDividerHeight

Added in API level 29
open fun setSelectionDividerHeight(height: Int): Unit

Set the height for the divider that separates the currently selected value from the others.

Parameters
height Int: The height to be set Value is 0 or greater The units of this value are pixels.

setTextColor

Added in API level 29
open fun setTextColor(color: Int): Unit

Sets the text color for all the states (normal, selected, focused) to be the given color.

Parameters
color Int: A color value in the form 0xAARRGGBB.

setTextSize

Added in API level 29
open fun setTextSize(size: Float): Unit

Sets the text size to the given value. This value must be > 0

Parameters
size Float: The size in pixel units. Value is 0.0 or greater

setValue

Added in API level 11
open fun setValue(value: Int): Unit

Set the current value for the number picker.

If the argument is less than the NumberPicker#getMinValue() and NumberPicker#getWrapSelectorWheel() is false the current value is set to the NumberPicker#getMinValue() value.

If the argument is less than the NumberPicker#getMinValue() and NumberPicker#getWrapSelectorWheel() is true the current value is set to the NumberPicker#getMaxValue() value.

If the argument is more than the NumberPicker#getMaxValue() and NumberPicker#getWrapSelectorWheel() is false the current value is set to the NumberPicker#getMaxValue() value.

If the argument is more than the NumberPicker#getMaxValue() and NumberPicker#getWrapSelectorWheel() is true the current value is set to the NumberPicker#getMinValue() value.

Parameters
value Int: The current value.

setWrapSelectorWheel

Added in API level 11
open fun setWrapSelectorWheel(wrapSelectorWheel: Boolean): Unit

Sets whether the selector wheel shown during flinging/scrolling should wrap around the NumberPicker#getMinValue() and NumberPicker#getMaxValue() values.

By default if the range (max - min) is more than the number of items shown on the selector wheel the selector wheel wrapping is enabled.

Note: If the number of items, i.e. the range ( getMaxValue() - getMinValue()) is less than the number of items shown on the selector wheel, the selector wheel will not wrap. Hence, in such a case calling this method is a NOP.

Parameters
wrapSelectorWheel Boolean: Whether to wrap.

Protected methods

computeVerticalScrollExtent

Added in API level 11
protected open fun computeVerticalScrollExtent(): Int
Return
Int the vertical extent of the scrollbar's thumb

computeVerticalScrollOffset

Added in API level 11
protected open fun computeVerticalScrollOffset(): Int
Return
Int the vertical offset of the scrollbar's thumb

computeVerticalScrollRange

Added in API level 11
protected open fun computeVerticalScrollRange(): Int
Return
Int the total vertical range represented by the vertical scrollbar

The default range is the drawing height of this view.

dispatchHoverEvent

Added in API level 14
protected open fun dispatchHoverEvent(event: MotionEvent!): Boolean
Parameters
event MotionEvent!: The motion event to be dispatched.
Return
Boolean True if the event was handled by the view, false otherwise.

drawableStateChanged

Added in API level 11
protected open fun drawableStateChanged(): Unit

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

If the View has a StateListAnimator, it will also be called to run necessary state change animations.

Be sure to call through to the superclass when overriding this function.
If you override this method you must call through to the superclass implementation. If you override this method you must call through to the superclass implementation.

getBottomFadingEdgeStrength

Added in API level 11
protected open fun getBottomFadingEdgeStrength(): Float
Return
Float the intensity of the bottom fade as a float between 0.0f and 1.0f

getTopFadingEdgeStrength

Added in API level 11
protected open fun getTopFadingEdgeStrength(): Float
Return
Float the intensity of the top fade as a float between 0.0f and 1.0f

onDetachedFromWindow

Added in API level 11
protected open fun onDetachedFromWindow(): Unit

onDraw

Added in API level 11
protected open fun onDraw(canvas: Canvas): Unit
Parameters
canvas Canvas: the canvas on which the background will be drawn This value cannot be null.

onLayout

Added in API level 11
protected open fun onLayout(
    changed: Boolean,
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Unit
Parameters
changed Boolean: This is a new size or position for this view
left Int: Left position, relative to parent
top Int: Top position, relative to parent
right Int: Right position, relative to parent
bottom Int: Bottom position, relative to parent

onMeasure

Added in API level 11
protected open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit
Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.