public class

NumberPicker

extends LinearLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.widget.NumberPicker

Class Overview

A widget that enables the user to select a number form a predefined range. The widget presents an input filed and up and down buttons for selecting the current value. Pressing/long pressing the up and down buttons increments and decrements the current value respectively. Touching the input filed shows a scroll wheel, tapping on which while shown and not moving allows direct edit of the current value. Sliding motions up or down hide the buttons and the input filed, show the scroll wheel, and rotate the latter. Flinging is also supported. The widget enables mapping from positions to strings such that instead the position index the corresponding string is displayed.

For an example of using this widget, see TimePicker.

Summary

Nested Classes
interface NumberPicker.Formatter Interface used to format current value into a string for presentation. 
interface NumberPicker.OnScrollListener Interface to listen for the picker scroll state. 
interface NumberPicker.OnValueChangeListener Interface to listen for changes of the current value. 
[Expand]
Inherited XML Attributes
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
NumberPicker(Context context)
Create a new number picker.
NumberPicker(Context context, AttributeSet attrs)
Create a new number picker.
NumberPicker(Context context, AttributeSet attrs, int defStyle)
Create a new number picker
Public Methods
void computeScroll()
Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary.
boolean dispatchKeyEvent(KeyEvent event)
Dispatch a key event to the next view on the focus path.
boolean dispatchTouchEvent(MotionEvent event)
Pass the touch screen motion event down to the target view, or this view if it is the target.
boolean dispatchTrackballEvent(MotionEvent event)
Pass a trackball motion event down to the focused view.
void draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
String[] getDisplayedValues()
Gets the values to be displayed instead of string values.
int getMaxValue()
Returns the max value of the picker.
int getMinValue()
Returns the min value of the picker.
int getSolidColor()
Override this if your view is known to always be drawn on top of a solid color background, and needs to draw fading edges.
int getValue()
Returns the value of the picker.
boolean getWrapSelectorWheel()
Gets whether the selector wheel wraps when reaching the min/max value.
boolean onInterceptTouchEvent(MotionEvent event)
Implement this method to intercept all touch screen motion events.
boolean onTouchEvent(MotionEvent ev)
Implement this method to handle touch screen motion events.
void scrollBy(int x, int y)
Move the scrolled position of your view.
void setDisplayedValues(String[] displayedValues)
Sets the values to be displayed.
void setEnabled(boolean enabled)
Set the enabled state of this view.
void setFormatter(NumberPicker.Formatter formatter)
Set the formatter to be used for formatting the current value.
void setMaxValue(int maxValue)
Sets the max value of the picker.
void setMinValue(int minValue)
Sets the min value of the picker.
void setOnLongPressUpdateInterval(long intervalMillis)
Sets the speed at which the numbers be incremented and decremented when the up and down buttons are long pressed respectively.
void setOnScrollListener(NumberPicker.OnScrollListener onScrollListener)
Set listener to be notified for scroll state changes.
void setOnValueChangedListener(NumberPicker.OnValueChangeListener onValueChangedListener)
Sets the listener to be notified on change of the current value.
void setValue(int value)
Set the current value for the number picker.
void setWrapSelectorWheel(boolean wrapSelector)
Sets whether the selector wheel shown during flinging/scrolling should wrap around the getMinValue() and getMaxValue() values.
Protected Methods
void dispatchDraw(Canvas canvas)
Called by draw to draw the child views.
float getBottomFadingEdgeStrength()
Returns the strength, or intensity, of the bottom faded edge.
float getTopFadingEdgeStrength()
Returns the strength, or intensity, of the top faded edge.
void onAttachedToWindow()
This is called when the view is attached to a window.
void onDetachedFromWindow()
This is called when the view is detached from a window.
void onDraw(Canvas canvas)
Implement this to do your drawing.
void onLayout(boolean changed, int left, int top, int right, int bottom)
Called from layout when this view should assign a size and position to each of its children.
[Expand]
Inherited Methods
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

Public Constructors

public NumberPicker (Context context)

Since: API Level 11

Create a new number picker.

Parameters
context The application environment.

public NumberPicker (Context context, AttributeSet attrs)

Since: API Level 11

Create a new number picker.

Parameters
context The application environment.
attrs A collection of attributes.

public NumberPicker (Context context, AttributeSet attrs, int defStyle)

Since: API Level 11

Create a new number picker

Parameters
context the application environment.
attrs a collection of attributes.
defStyle The default style to apply to this view.

Public Methods

public void computeScroll ()

Since: API Level 11

Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary. This will typically be done if the child is animating a scroll using a Scroller object.

public boolean dispatchKeyEvent (KeyEvent event)

Since: API Level 11

Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.

Parameters
event The key event to be dispatched.
Returns
  • True if the event was handled, false otherwise.

public boolean dispatchTouchEvent (MotionEvent event)

Since: API Level 11

Pass the touch screen motion event down to the target view, or this view if it is the target.

Parameters
event The motion event to be dispatched.
Returns
  • True if the event was handled by the view, false otherwise.

public boolean dispatchTrackballEvent (MotionEvent event)

Since: API Level 11

Pass a trackball motion event down to the focused view.

Parameters
event The motion event to be dispatched.
Returns
  • True if the event was handled by the view, false otherwise.

public void draw (Canvas canvas)

Since: API Level 11

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Parameters
canvas The Canvas to which the View is rendered.

public String[] getDisplayedValues ()

Since: API Level 11

Gets the values to be displayed instead of string values.

Returns
  • The displayed values.

public int getMaxValue ()

Since: API Level 11

Returns the max value of the picker.

Returns
  • The max value.

public int getMinValue ()

Since: API Level 11

Returns the min value of the picker.

Returns
  • The min value

public int getSolidColor ()

Since: API Level 11

Override this if your view is known to always be drawn on top of a solid color background, and needs to draw fading edges. Returning a non-zero color enables the view system to optimize the drawing of the fading edges. If you do return a non-zero color, the alpha should be set to 0xFF.

Returns
  • The known solid color background for this view, or 0 if the color may vary

public int getValue ()

Since: API Level 11

Returns the value of the picker.

Returns
  • The value.

public boolean getWrapSelectorWheel ()

Since: API Level 11

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

Returns
  • True if the selector wheel wraps.

public boolean onInterceptTouchEvent (MotionEvent event)

Since: API Level 11

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.

Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

Parameters
event The motion event being dispatched down the hierarchy.
Returns
  • 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.

public boolean onTouchEvent (MotionEvent ev)

Since: API Level 11

Implement this method to handle touch screen motion events.

Parameters
ev The motion event.
Returns
  • True if the event was handled, false otherwise.

public void scrollBy (int x, int y)

Since: API Level 11

Move the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated.

Parameters
x the amount of pixels to scroll by horizontally
y the amount of pixels to scroll by vertically

public void setDisplayedValues (String[] displayedValues)

Since: API Level 11

Sets the values to be displayed.

Parameters
displayedValues The displayed values.

public void setEnabled (boolean enabled)

Since: API Level 11

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

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

public void setFormatter (NumberPicker.Formatter formatter)

Since: API Level 11

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 The formatter object. If formatter is null, valueOf(int) will be used.

public void setMaxValue (int maxValue)

Since: API Level 11

Sets the max value of the picker.

Parameters
maxValue The max value.

public void setMinValue (int minValue)

Since: API Level 11

Sets the min value of the picker.

Parameters
minValue The min value.

public void setOnLongPressUpdateInterval (long intervalMillis)

Since: API Level 11

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 The speed (in milliseconds) at which the numbers will be incremented and decremented.

public void setOnScrollListener (NumberPicker.OnScrollListener onScrollListener)

Since: API Level 11

Set listener to be notified for scroll state changes.

Parameters
onScrollListener The listener.

public void setOnValueChangedListener (NumberPicker.OnValueChangeListener onValueChangedListener)

Since: API Level 11

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

Parameters
onValueChangedListener The listener.

public void setValue (int value)

Since: API Level 11

Set the current value for the number picker.

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

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

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

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

Parameters
value The current value.

public void setWrapSelectorWheel (boolean wrapSelector)

Since: API Level 11

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

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

Parameters
wrapSelector Whether to wrap.

Protected Methods

protected void dispatchDraw (Canvas canvas)

Since: API Level 11

Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

Parameters
canvas the canvas on which to draw the view

protected float getBottomFadingEdgeStrength ()

Since: API Level 11

Returns the strength, or intensity, of the bottom faded edge. The strength is a value between 0.0 (no fade) and 1.0 (full fade). The default implementation returns 0.0 or 1.0 but no value in between. Subclasses should override this method to provide a smoother fade transition when scrolling occurs.

Returns
  • the intensity of the bottom fade as a float between 0.0f and 1.0f

protected float getTopFadingEdgeStrength ()

Since: API Level 11

Returns the strength, or intensity, of the top faded edge. The strength is a value between 0.0 (no fade) and 1.0 (full fade). The default implementation returns 0.0 or 1.0 but no value in between. Subclasses should override this method to provide a smoother fade transition when scrolling occurs.

Returns
  • the intensity of the top fade as a float between 0.0f and 1.0f

protected void onAttachedToWindow ()

Since: API Level 11

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).

protected void onDetachedFromWindow ()

Since: API Level 11

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

protected void onDraw (Canvas canvas)

Since: API Level 11

Implement this to do your drawing.

Parameters
canvas the canvas on which the background will be drawn

protected void onLayout (boolean changed, int left, int top, int right, int bottom)

Since: API Level 11

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed This is a new size or position for this view
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent