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!, mode: Int)

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

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.

Public methods
open CharSequence!

open Int

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)

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

open Unit

open Parcelable?

open Boolean

open Boolean

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)

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

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

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

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!,
    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.

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 1
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 1
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 1
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.

Public methods

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

getBaseline

Added in API level 1
open fun getBaseline(): Int
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
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!
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
Parameters
state Parcelable!: The frozen state that had previously been returned by onSaveInstanceState.

onSaveInstanceState

Added in API level 1
open fun onSaveInstanceState(): Parcelable?
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
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
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
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

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
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.