public class

Spinner

extends AbsSpinner
implements DialogInterface.OnClickListener
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.AdapterView<T extends android.widget.Adapter>
         ↳ android.widget.AbsSpinner
           ↳ android.widget.Spinner

Class Overview

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

Summary

XML Attributes
Attribute Name Related Method Description
android:gravity setGravity(int) Gravity setting for positioning the currently selected item. 
android:prompt The prompt to display when the spinner's dialog is shown. 
[Expand]
Inherited XML Attributes
From class android.widget.AbsSpinner
From class android.view.ViewGroup
From class android.view.View
Constants
int MODE_DIALOG Use a dialog window for selecting spinner options.
int MODE_DROPDOWN Use a dropdown anchored to the Spinner for selecting spinner options.
[Expand]
Inherited Constants
From class android.widget.AdapterView
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
Spinner(Context context)
Construct a new spinner with the given context's theme.
Spinner(Context context, int mode)
Construct a new spinner with the given context's theme and the supplied mode of displaying choices.
Spinner(Context context, AttributeSet attrs)
Construct a new spinner with the given context's theme and the supplied attribute set.
Spinner(Context context, AttributeSet attrs, int defStyle)
Construct a new spinner with the given context's theme, the supplied attribute set, and default style.
Spinner(Context context, AttributeSet attrs, int defStyle, int mode)
Construct a new spinner with the given context's theme, the supplied attribute set, and default style.
Public Methods
int getBaseline()

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

CharSequence getPrompt()
void onClick(DialogInterface dialog, int which)
This method will be invoked when a button in the dialog is clicked.
boolean performClick()
Call this view's OnClickListener, if it is defined.
void setAdapter(SpinnerAdapter adapter)
The Adapter is used to provide the data which backs this Spinner.
void setGravity(int gravity)
Describes how the selected item view is positioned.
void setOnItemClickListener(AdapterView.OnItemClickListener l)

A spinner does not support item click events.

void setPrompt(CharSequence prompt)
Sets the prompt to display when the dialog is shown.
void setPromptId(int promptId)
Sets the prompt to display when the dialog is shown.
Protected Methods
void onDetachedFromWindow()
This is called when the view is detached from a window.
void onLayout(boolean changed, int l, int t, int r, int b)
Called from layout when this view should assign a size and position to each of its children.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

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

[Expand]
Inherited Methods
From class android.widget.AbsSpinner
From class android.widget.AdapterView
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.content.DialogInterface.OnClickListener
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

XML Attributes

android:gravity

Gravity setting for positioning the currently selected item.

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

ConstantValueDescription
top0x30 Push object to the top of its container, not changing its size.
bottom0x50 Push object to the bottom of its container, not changing its size.
left0x03 Push object to the left of its container, not changing its size.
right0x05 Push object to the right of its container, not changing its size.
center_vertical0x10 Place object in the vertical center of its container, not changing its size.
fill_vertical0x70 Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal0x01 Place object in the horizontal center of its container, not changing its size.
fill_horizontal0x07 Grow the horizontal size of the object if needed so it completely fills its container.
center0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical0x80 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.
clip_horizontal0x08 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.
start0x00800003 Push object to the beginning of its container, not changing its size.
end0x00800005 Push object to the end of its container, not changing its size.

This corresponds to the global attribute resource symbol gravity.

Related Methods

android:prompt

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

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

This corresponds to the global attribute resource symbol prompt.

Related Methods

Constants

public static final int MODE_DIALOG

Since: API Level 11

Use a dialog window for selecting spinner options.

Constant Value: 0 (0x00000000)

public static final int MODE_DROPDOWN

Since: API Level 11

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

Constant Value: 1 (0x00000001)

Public Constructors

public Spinner (Context context)

Since: API Level 1

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

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

public Spinner (Context context, int mode)

Since: API Level 11

Construct 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 The Context the view is running in, through which it can access the current theme, resources, etc.
mode Constant describing how the user will select choices from the spinner.

public Spinner (Context context, AttributeSet attrs)

Since: API Level 1

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

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

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

Since: API Level 1

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

Parameters
context The Context the view is running in, through which it can access the current theme, resources, etc.
attrs The attributes of the XML tag that is inflating the view.
defStyle The default style to apply to this view. If 0, no style will be applied (beyond what is included in the theme). This may either be an attribute resource, whose value will be retrieved from the current theme, or an explicit style resource.

public Spinner (Context context, AttributeSet attrs, int defStyle, int mode)

Since: API Level 11

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

Parameters
context The Context the view is running in, through which it can access the current theme, resources, etc.
attrs The attributes of the XML tag that is inflating the view.
defStyle The default style to apply to this view. If 0, no style will be applied (beyond what is included in the theme). This may either be an attribute resource, whose value will be retrieved from the current theme, or an explicit style resource.
mode Constant describing how the user will select choices from the spinner.

Public Methods

public int getBaseline ()

Since: API Level 1

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
  • the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported

public CharSequence getPrompt ()

Since: API Level 1

Returns
  • The prompt to display when the dialog is shown

public void onClick (DialogInterface dialog, int which)

Since: API Level 1

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

Parameters
dialog The dialog that received the click.
which The button that was clicked (e.g. BUTTON1) or the position of the item clicked.

public boolean performClick ()

Since: API Level 1

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

Returns
  • True there was an assigned OnClickListener that was called, false otherwise is returned.

public void setAdapter (SpinnerAdapter adapter)

Since: API Level 1

The Adapter is used to provide the data which backs this Spinner. It also provides methods to transform spinner items based on their position relative to the selected item.

Parameters
adapter The SpinnerAdapter to use for this Spinner

public void setGravity (int gravity)

Since: API Level 11

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

Related XML Attributes
Parameters
gravity See Gravity

public void setOnItemClickListener (AdapterView.OnItemClickListener l)

Since: API Level 1

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

Parameters
l this listener will be ignored

public void setPrompt (CharSequence prompt)

Since: API Level 1

Sets the prompt to display when the dialog is shown.

Parameters
prompt the prompt to set

public void setPromptId (int promptId)

Since: API Level 1

Sets the prompt to display when the dialog is shown.

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

Protected Methods

protected void onDetachedFromWindow ()

Since: API Level 1

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

protected void onLayout (boolean changed, int l, int t, int r, int b)

Since: API Level 1

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
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Since: API Level 1

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 overriden 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 horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.