Added in API level 1

Spinner


open class Spinner : AbsSpinner, DialogInterface.OnClickListener
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.AdapterView<android.widget.SpinnerAdapter>
   ↳ android.widget.AbsSpinner
   ↳ android.widget.Spinner

A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.

See the Spinners guide.

Summary

XML attributes
android:dropDownHorizontalOffset Amount of pixels by which the drop down should be offset horizontally.
android:dropDownSelector List selector to use for spinnerMode="dropdown" display.
android:dropDownVerticalOffset Amount of pixels by which the drop down should be offset vertically.
android:dropDownWidth Width of the dropdown in spinnerMode="dropdown".
android:gravity Gravity setting for positioning the currently selected item.
android:popupBackground Background drawable to use for the dropdown in spinnerMode="dropdown".
android:prompt The prompt to display when the spinner's dialog is shown.
android:spinnerMode Display mode for spinner options.
Inherited XML attributes
Constants
static Int

Use a dialog window for selecting spinner options.

static Int

Use a dropdown anchored to the Spinner for selecting spinner options.

Inherited constants
Public constructors
Spinner(context: Context!)

Constructs a new spinner with the given context's theme.

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

Constructs a new spinner with the given context's theme and the supplied attribute set.

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

Constructs a new spinner with the given context's theme, the supplied attribute set, and default style attribute.

Spinner(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, mode: Int)

Constructs a new spinner with the given context's theme, the supplied attribute set, and default style attribute.

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

Constructs a new spinner with the given context's theme, the supplied attribute set, and default styles.

Spinner(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int, mode: Int, popupTheme: Resources.Theme!)

Constructs a new spinner with the given context, the supplied attribute set, default styles, popup mode (one of MODE_DIALOG or MODE_DROPDOWN), and the theme against which the popup should be inflated.

Spinner(context: Context!, mode: Int)

Constructs a new spinner with the given context's theme and the supplied mode of displaying choices.

Public methods
open CharSequence!

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

open Int

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

open Int

Get the configured horizontal offset in pixels for the spinner's popup window of choices.

open Int

Get the configured vertical offset in pixels for the spinner's popup window of choices.

open Int

Get the configured width of the spinner's popup window of choices in pixels.

open Int

Describes how the selected item view is positioned.

open Drawable!

Get the background drawable for the spinner's popup window of choices.

open Context!

open CharSequence!

open Unit
onClick(dialog: DialogInterface!, which: Int)

This method will be invoked when a button in the dialog is clicked.

open PointerIcon!
onResolvePointerIcon(event: MotionEvent!, pointerIndex: Int)

Resolve the pointer icon that should be used for specified pointer in the motion event.

open Unit

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

open Parcelable?

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.

open Boolean

Implement this method to handle pointer events.

open Boolean

Call this view's OnClickListener, if it is defined.

open Unit

Sets the SpinnerAdapter used to provide the data which backs this Spinner.

open Unit

Set a horizontal offset in pixels for the spinner's popup window of choices.

open Unit

Set a vertical offset in pixels for the spinner's popup window of choices.

open Unit

Set the width of the spinner's popup window of choices in pixels.

open Unit
setEnabled(enabled: Boolean)

Set the enabled state of this view.

open Unit
setGravity(gravity: Int)

Describes how the selected item view is positioned.

open Unit

A spinner does not support item click events.

open Unit

Set the background drawable for the spinner's popup window of choices.

open Unit

Set the background drawable for the spinner's popup window of choices.

open Unit

Sets the prompt to display when the dialog is shown.

open Unit
setPromptId(promptId: Int)

Sets the prompt to display when the dialog is shown.

Protected methods
open Unit

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

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

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

Measure the view and its content to determine the measured width and the measured height.

Inherited functions
Inherited properties

XML attributes

android:dropDownHorizontalOffset

android:dropDownHorizontalOffset
Amount of pixels by which the drop down should be offset horizontally.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:dropDownSelector

android:dropDownSelector
List selector to use for spinnerMode="dropdown" display.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

May be a color value, in the form of "rgb", "argb", "rrggbb", or "aarrggbb".

android:dropDownVerticalOffset

android:dropDownVerticalOffset
Amount of pixels by which the drop down should be offset vertically.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:dropDownWidth

android:dropDownWidth
Width of the dropdown in spinnerMode="dropdown".

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Must be one of the following constant values.

Constant Value Description
fill_parent ffffffff The dropdown should fill the width of the screen. This constant is deprecated starting from API Level 8 and is replaced by match_parent.
match_parent ffffffff The dropdown should fit the width of the screen. Introduced in API Level 8.
wrap_content fffffffe The dropdown should fit the width of its anchor.

android:gravity

android:gravity
Gravity setting for positioning the currently selected item.

Must be one or more (separated by '|') of the following constant values.

Constant Value Description
bottom 50 Push object to the bottom of its container, not changing its size.
center 11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontal 1 Place object in the horizontal center of its container, not changing its size.
center_vertical 10 Place object in the vertical center of its container, not changing its size.
clip_horizontal 8 Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
clip_vertical 80 Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
end 800005 Push object to the end of its container, not changing its size.
fill 77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontal 7 Grow the horizontal size of the object if needed so it completely fills its container.
fill_vertical 70 Grow the vertical size of the object if needed so it completely fills its container.
left 3 Push object to the left of its container, not changing its size.
right 5 Push object to the right of its container, not changing its size.
start 800003 Push object to the beginning of its container, not changing its size.
top 30 Push object to the top of its container, not changing its size.

android:popupBackground

android:popupBackground
Background drawable to use for the dropdown in spinnerMode="dropdown".

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

May be a color value, in the form of "rgb", "argb", "rrggbb", or "aarrggbb".

android:prompt

android:prompt
The prompt to display when the spinner's dialog is shown.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:spinnerMode

android:spinnerMode
Display mode for spinner options.

Must be one of the following constant values.

Constant Value Description
dialog 0 Spinner options will be presented to the user as a dialog window.
dropdown 1 Spinner options will be presented to the user as an inline dropdown anchored to the spinner widget itself.

Constants

MODE_DIALOG

Added in API level 11
static val MODE_DIALOG: Int

Use a dialog window for selecting spinner options.

Value: 0

MODE_DROPDOWN

Added in API level 11
static val MODE_DROPDOWN: Int

Use a dropdown anchored to the Spinner for selecting spinner options.

Value: 1

Public constructors

Spinner

Added in API level 1
Spinner(context: Context!)

Constructs a new spinner with the given context's theme.

Parameters
context Context!: The Context the view is running in, through which it can access the current theme, resources, etc.

Spinner

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

Constructs a new spinner with the given context's theme and the supplied attribute set.

Parameters
context Context!: The Context the view is running in, through which it can access the current theme, resources, etc.
attrs AttributeSet!: The attributes of the XML tag that is inflating the view.

Spinner

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

Constructs a new spinner with the given context's theme, the supplied attribute set, and default style attribute.

Parameters
context Context!: The Context the view is running in, through which it can access the current theme, resources, etc.
attrs AttributeSet!: The attributes of the XML tag that is inflating the view.
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.

Spinner

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

Constructs a new spinner with the given context's theme, the supplied attribute set, and default style attribute. mode may be one of MODE_DIALOG or MODE_DROPDOWN and determines how the user will select choices from the spinner.

Parameters
context Context!: The Context the view is running in, through which it can access the current theme, resources, etc.
attrs AttributeSet!: The attributes of the XML tag that is inflating the view.
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.
mode Int: Constant describing how the user will select choices from the spinner.

Spinner

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

Constructs a new spinner with the given context's theme, the supplied attribute set, and default styles. mode may be one of MODE_DIALOG or MODE_DROPDOWN and determines how the user will select choices from the spinner.

Parameters
context Context!: The Context the view is running in, through which it can access the current theme, resources, etc.
attrs AttributeSet!: The attributes of the XML tag that is inflating the view.
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.
mode Int: Constant describing how the user will select choices from the spinner.

Spinner

Added in API level 23
Spinner(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int,
    mode: Int,
    popupTheme: Resources.Theme!)

Constructs a new spinner with the given context, the supplied attribute set, default styles, popup mode (one of MODE_DIALOG or MODE_DROPDOWN), and the theme against which the popup should be inflated.

Parameters
context Context!: The context against which the view is inflated, which provides access to the current theme, resources, etc.
attrs AttributeSet!: The attributes of the XML tag that is inflating the view.
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.
mode Int: Constant describing how the user will select choices from the spinner.
popupTheme Resources.Theme!: The theme against which the dialog or dropdown popup should be inflated. May be null to use the view theme. If set, this will override any value specified by android.R.styleable#Spinner_popupTheme.

Spinner

Added in API level 11
Spinner(
    context: Context!,
    mode: Int)

Constructs a new spinner with the given context's theme and the supplied mode of displaying choices. mode may be one of MODE_DIALOG or MODE_DROPDOWN.

Parameters
context Context!: The Context the view is running in, through which it can access the current theme, resources, etc.
mode Int: Constant describing how the user will select choices from the spinner.

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.

getBaseline

Added in API level 1
open fun getBaseline(): Int

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.

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

getDropDownHorizontalOffset

Added in API level 16
open fun getDropDownHorizontalOffset(): Int

Get the configured horizontal offset in pixels for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; other modes will return 0.

Return
Int Horizontal offset in pixels

getDropDownVerticalOffset

Added in API level 16
open fun getDropDownVerticalOffset(): Int

Get the configured vertical offset in pixels for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; other modes will return 0.

Return
Int Vertical offset in pixels

getDropDownWidth

Added in API level 16
open fun getDropDownWidth(): Int

Get the configured width of the spinner's popup window of choices in pixels. The returned value may also be android.view.ViewGroup.LayoutParams#MATCH_PARENT meaning the popup window will match the width of the Spinner itself, or android.view.ViewGroup.LayoutParams#WRAP_CONTENT to wrap to the measured size of contained dropdown list items.

Return
Int Width in pixels, WRAP_CONTENT, or MATCH_PARENT

getGravity

Added in API level 16
open fun getGravity(): Int

Describes how the selected item view is positioned. The default is determined by the current theme.

Return
Int A Gravity value

getPopupBackground

Added in API level 16
open fun getPopupBackground(): Drawable!

Get the background drawable for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; other modes will return null.

Return
Drawable! background Background drawable

getPopupContext

Added in API level 23
open fun getPopupContext(): Context!
Return
Context! the context used to inflate the Spinner's popup or dialog window

getPrompt

Added in API level 1
open fun getPrompt(): CharSequence!
Return
CharSequence! The prompt to display when the dialog is shown

onClick

Added in API level 1
open fun onClick(
    dialog: DialogInterface!,
    which: Int
): Unit

This method will be invoked when a button in the dialog is clicked.

Parameters
dialog DialogInterface!: the dialog that received the click
which Int: the button that was clicked (ex. DialogInterface.BUTTON_POSITIVE) or the position of the item clicked

onResolvePointerIcon

Added in API level 24
open fun onResolvePointerIcon(
    event: MotionEvent!,
    pointerIndex: Int
): PointerIcon!

Resolve the pointer icon that should be used for specified pointer in the motion event. The default implementation will resolve the pointer icon to one set using setPointerIcon(android.view.PointerIcon) for mouse devices. Subclasses may override this to customize the icon for the given pointer. For example, to always show the PointerIcon.TYPE_HANDWRITING icon for a stylus pointer, the event can be resolved in the following way:

@Override
  public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
      final int toolType = event.getToolType(pointerIndex);
      if (!event.isFromSource(InputDevice.SOURCE_MOUSE)
              && event.isFromSource(InputDevice.SOURCE_STYLUS)
              && (toolType == MotionEvent.TOOL_TYPE_STYLUS
                      || toolType == MotionEvent.TOOL_TYPE_ERASER)) {
          // Show this pointer icon only if this pointer is a stylus.
          return PointerIcon.getSystemIcon(mContext, PointerIcon.TYPE_HANDWRITING);
      }
      // Use the default logic for determining the pointer icon for other non-stylus pointers,
      // like for the mouse cursor.
      return super.onResolvePointerIcon(event, pointerIndex);
  }
  

Parameters
event MotionEvent!: The MotionEvent that requires a pointer icon to be resolved for one of pointers.
pointerIndex Int: The index of the pointer in event for which to retrieve the PointerIcon. This will be between 0 and MotionEvent.getPointerCount().
Return
PointerIcon! the pointer icon to use for specified pointer, or null if a pointer icon is not specified and the default icon should be used.

onRestoreInstanceState

Added in API level 1
open fun onRestoreInstanceState(state: Parcelable!): Unit

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.
If you override this method you must call through to the superclass implementation.

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

onSaveInstanceState

Added in API level 1
open fun onSaveInstanceState(): Parcelable?

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.
If you override this method you must call through to the superclass implementation.

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

onTouchEvent

Added in API level 1
open fun onTouchEvent(event: MotionEvent!): Boolean

Implement this method to handle pointer events.

This method is called to handle motion events where pointers are down on the view. For example, this could include touchscreen touches, stylus touches, or click-and-drag events from a mouse. However, it is not called for motion events that do not involve pointers being down, such as hover events or mouse scroll wheel movements.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled
Parameters
event MotionEvent!: The motion event.
Return
Boolean True if the event was handled, false otherwise.

performClick

Added in API level 1
open fun performClick(): Boolean

Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.

Return
Boolean True there was an assigned OnClickListener that was called, false otherwise is returned.

setAdapter

Added in API level 1
open fun setAdapter(adapter: SpinnerAdapter!): Unit

Sets the SpinnerAdapter used to provide the data which backs this Spinner.

If this Spinner has a popup theme set in XML via the popupTheme attribute, the adapter should inflate drop-down views using the same theme. The easiest way to achieve this is by using getPopupContext() to obtain a layout inflater for use in SpinnerAdapter.getDropDownView(int, View, ViewGroup).

Spinner overrides Adapter.getViewTypeCount() on the Adapter associated with this view. Calling getItemViewType(int) on the object returned from getAdapter() will always return 0. Calling getViewTypeCount() will always return 1. On API Build.VERSION_CODES.LOLLIPOP and above, attempting to set an adapter with more than one view type will throw an IllegalArgumentException.

Parameters
adapter SpinnerAdapter!: the adapter to set
Exceptions
java.lang.IllegalArgumentException if the adapter has more than one view type

setDropDownHorizontalOffset

Added in API level 16
open fun setDropDownHorizontalOffset(pixels: Int): Unit

Set a horizontal offset in pixels for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; this method is a no-op in other modes.

Parameters
pixels Int: Horizontal offset in pixels

setDropDownVerticalOffset

Added in API level 16
open fun setDropDownVerticalOffset(pixels: Int): Unit

Set a vertical offset in pixels for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; this method is a no-op in other modes.

Parameters
pixels Int: Vertical offset in pixels

setDropDownWidth

Added in API level 16
open fun setDropDownWidth(pixels: Int): Unit

Set the width of the spinner's popup window of choices in pixels. This value may also be set to android.view.ViewGroup.LayoutParams#MATCH_PARENT to match the width of the Spinner itself, or android.view.ViewGroup.LayoutParams#WRAP_CONTENT to wrap to the measured size of contained dropdown list items.

Only valid in MODE_DROPDOWN; this method is a no-op in other modes.

Parameters
pixels Int: Width in pixels, WRAP_CONTENT, or MATCH_PARENT

setEnabled

Added in API level 1
open fun setEnabled(enabled: Boolean): Unit

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.

setGravity

Added in API level 11
open fun setGravity(gravity: Int): Unit

Describes how the selected item view is positioned. Currently only the horizontal component is used. The default is determined by the current theme.

Parameters
gravity Int: See android.view.Gravity

setOnItemClickListener

Added in API level 1
open fun setOnItemClickListener(l: AdapterView.OnItemClickListener?): Unit

A spinner does not support item click events. Calling this method will raise an exception.

Instead use AdapterView.setOnItemSelectedListener.

Parameters
listener The callback that will be invoked.
This value may be null.
l AdapterView.OnItemClickListener?: this listener will be ignored

setPopupBackgroundDrawable

Added in API level 16
open fun setPopupBackgroundDrawable(background: Drawable!): Unit

Set the background drawable for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; this method is a no-op in other modes.

Parameters
background Drawable!: Background drawable

setPopupBackgroundResource

Added in API level 16
open fun setPopupBackgroundResource(resId: Int): Unit

Set the background drawable for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; this method is a no-op in other modes.

Parameters
resId Int: Resource ID of a background drawable

setPrompt

Added in API level 1
open fun setPrompt(prompt: CharSequence!): Unit

Sets the prompt to display when the dialog is shown.

Parameters
prompt CharSequence!: the prompt to set

setPromptId

Added in API level 1
open fun setPromptId(promptId: Int): Unit

Sets the prompt to display when the dialog is shown.

Parameters
promptId Int: the resource ID of the prompt to display when the dialog is shown

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.

onLayout

Added in API level 1
protected open fun onLayout(
    changed: Boolean,
    l: Int,
    t: Int,
    r: Int,
    b: Int
): Unit
Parameters
changed Boolean: 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

onMeasure

Added in API level 1
protected open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int,int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int,int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int,int). Calling the superclass' onMeasure(int,int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int,int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

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.