public
class
Spinner
extends AbsSpinner
implements
DialogInterface.OnClickListener
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.view.ViewGroup | ||||
↳ | android.widget.AdapterView<android.widget.SpinnerAdapter> | ||||
↳ | android.widget.AbsSpinner | ||||
↳ | android.widget.Spinner |
Known Direct Subclasses |
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.
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 | |
---|---|
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. |
Inherited constants |
---|
From
class
android.widget.AdapterView
|
From
class
android.view.ViewGroup
|
From
class
android.view.View
|
Inherited fields |
---|
From
class
android.view.View
|
Public constructors | |
---|---|
Spinner(Context context)
Constructs a new spinner with the given context's theme. |
|
Spinner(Context context, int mode)
Constructs a new spinner with the given context's theme and the supplied mode of displaying choices. |
|
Spinner(Context context, AttributeSet attrs)
Constructs a new spinner with the given context's theme and the supplied attribute set. |
|
Spinner(Context context, AttributeSet attrs, int defStyleAttr)
Constructs a new spinner with the given context's theme, the supplied attribute set, and default style attribute. |
|
Spinner(Context context, AttributeSet attrs, int defStyleAttr, int mode)
Constructs a new spinner with the given context's theme, the supplied attribute set, and default style attribute. |
|
Spinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int mode)
Constructs a new spinner with the given context's theme, the supplied attribute set, and default styles. |
|
Spinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int mode, Resources.Theme popupTheme)
Constructs a new spinner with the given context, the supplied attribute
set, default styles, popup mode (one of |
Public methods | |
---|---|
CharSequence
|
getAccessibilityClassName()
Return the class name of this object to be used for accessibility purposes. |
int
|
getBaseline()
Return the offset of the widget's text baseline from the widget's top boundary. |
int
|
getDropDownHorizontalOffset()
Get the configured horizontal offset in pixels for the spinner's popup window of choices. |
int
|
getDropDownVerticalOffset()
Get the configured vertical offset in pixels for the spinner's popup window of choices. |
int
|
getDropDownWidth()
Get the configured width of the spinner's popup window of choices in pixels. |
int
|
getGravity()
Describes how the selected item view is positioned. |
Drawable
|
getPopupBackground()
Get the background drawable for the spinner's popup window of choices. |
Context
|
getPopupContext()
|
CharSequence
|
getPrompt()
|
void
|
onClick(DialogInterface dialog, int which)
This method will be invoked when a button in the dialog is clicked. |
void
|
onRestoreInstanceState(Parcelable state)
Hook allowing a view to re-apply a representation of its internal state that had previously
been generated by |
Parcelable
|
onSaveInstanceState()
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. |
boolean
|
onTouchEvent(MotionEvent event)
Implement this method to handle touch screen motion events. |
boolean
|
performClick()
Call this view's OnClickListener, if it is defined. |
void
|
setAdapter(SpinnerAdapter adapter)
Sets the |
void
|
setDropDownHorizontalOffset(int pixels)
Set a horizontal offset in pixels for the spinner's popup window of choices. |
void
|
setDropDownVerticalOffset(int pixels)
Set a vertical offset in pixels for the spinner's popup window of choices. |
void
|
setDropDownWidth(int pixels)
Set the width of the spinner's popup window of choices in pixels. |
void
|
setEnabled(boolean enabled)
Set the enabled state of this view. |
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
|
setPopupBackgroundDrawable(Drawable background)
Set the background drawable for the spinner's popup window of choices. |
void
|
setPopupBackgroundResource(int resId)
Set the background drawable for the spinner's popup window of choices. |
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. |
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.view.ViewParent
| |
From
interface
android.view.ViewManager
| |
From
interface
android.graphics.drawable.Drawable.Callback
| |
From
interface
android.view.KeyEvent.Callback
| |
From
interface
android.view.accessibility.AccessibilityEventSource
| |
From
interface
android.content.DialogInterface.OnClickListener
|
Amount of pixels by which the drop down should be offset horizontally.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol dropDownHorizontalOffset
.
Related methods:
List selector to use for spinnerMode="dropdown" display.
May be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
May be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This corresponds to the global attribute
resource symbol dropDownSelector
.
Amount of pixels by which the drop down should be offset vertically.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol dropDownVerticalOffset
.
Related methods:
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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
May be one of the following constant values.
Constant | Value | Description |
---|---|---|
fill_parent | -1 | 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 | -1 | The dropdown should fit the width of the screen. Introduced in API Level 8. |
wrap_content | -2 | The dropdown should fit the width of its anchor. |
This corresponds to the global attribute
resource symbol dropDownWidth
.
Related methods:
Gravity setting for positioning the currently selected item.
Must be one or more (separated by '|') of the following constant values.
Constant | Value | Description |
---|---|---|
top | 0x30 | Push object to the top of its container, not changing its size. |
bottom | 0x50 | Push object to the bottom of its container, not changing its size. |
left | 0x03 | Push object to the left of its container, not changing its size. |
right | 0x05 | Push object to the right of its container, not changing its size. |
center_vertical | 0x10 | Place object in the vertical center of its container, not changing its size. |
fill_vertical | 0x70 | Grow the vertical size of the object if needed so it completely fills its container. |
center_horizontal | 0x01 | Place object in the horizontal center of its container, not changing its size. |
fill_horizontal | 0x07 | Grow the horizontal size of the object if needed so it completely fills its container. |
center | 0x11 | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. |
fill | 0x77 | Grow the horizontal and vertical size of the object if needed so it completely fills its container. |
clip_vertical | 0x80 | 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_horizontal | 0x08 | 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. |
start | 0x00800003 | Push object to the beginning of its container, not changing its size. |
end | 0x00800005 | Push object to the end of its container, not changing its size. |
This corresponds to the global attribute
resource symbol gravity
.
Related methods:
Background drawable to use for the dropdown in spinnerMode="dropdown".
May be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
May be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This corresponds to the global attribute
resource symbol popupBackground
.
Related methods:
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
.
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. |
This corresponds to the global attribute
resource symbol spinnerMode
.
int MODE_DIALOG
Use a dialog window for selecting spinner options.
Constant Value: 0 (0x00000000)
int MODE_DROPDOWN
Use a dropdown anchored to the Spinner for selecting spinner options.
Constant Value: 1 (0x00000001)
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 (Context context, int mode)
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. |
See also:
Spinner (Context context, AttributeSet attrs)
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 (Context context, AttributeSet attrs, int defStyleAttr)
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 (Context context, AttributeSet attrs, int defStyleAttr, int mode)
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. |
See also:
Spinner (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int mode)
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. |
See also:
Spinner (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int mode, Resources.Theme popupTheme)
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
Spinner_popupTheme . |
See also:
CharSequence getAccessibilityClassName ()
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
.
Returns | |
---|---|
CharSequence |
int getBaseline ()
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 | |
---|---|
int |
the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported |
int getDropDownHorizontalOffset ()
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.
Related XML Attributes:
Returns | |
---|---|
int |
Horizontal offset in pixels |
int getDropDownVerticalOffset ()
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.
Related XML Attributes:
Returns | |
---|---|
int |
Vertical offset in pixels |
int getDropDownWidth ()
Get the configured width of the spinner's popup window of choices in pixels.
The returned value may also be MATCH_PARENT
meaning the popup window will match the width of the Spinner itself, or
WRAP_CONTENT
to wrap to the measured size
of contained dropdown list items.
Related XML Attributes:
Returns | |
---|---|
int |
Width in pixels, WRAP_CONTENT, or MATCH_PARENT |
int getGravity ()
Describes how the selected item view is positioned. The default is determined by the current theme.
Returns | |
---|---|
int |
A Gravity value
|
Drawable getPopupBackground ()
Get the background drawable for the spinner's popup window of choices.
Only valid in MODE_DROPDOWN
; other modes will return null.
Related XML Attributes:
Returns | |
---|---|
Drawable |
background Background drawable |
Context getPopupContext ()
Returns | |
---|---|
Context |
the context used to inflate the Spinner's popup or dialog window |
CharSequence getPrompt ()
Returns | |
---|---|
CharSequence |
The prompt to display when the dialog is shown |
void onClick (DialogInterface dialog, int which)
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 (e.g.
BUTTON1 ) or the position
of the item clicked.
|
void onRestoreInstanceState (Parcelable state)
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.
Parameters | |
---|---|
state |
Parcelable :
The frozen state that had previously been returned by
onSaveInstanceState() . |
Parcelable onSaveInstanceState ()
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.
Returns | |
---|---|
Parcelable |
Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null. |
boolean onTouchEvent (MotionEvent event)
Implement this method to handle touch screen motion events.
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:
ACTION_CLICK
when
accessibility features are enabled
Parameters | |
---|---|
event |
MotionEvent :
The motion event. |
Returns | |
---|---|
boolean |
True if the event was handled, false otherwise. |
boolean performClick ()
Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.
Returns | |
---|---|
boolean |
True there was an assigned OnClickListener that was called, false otherwise is returned. |
void setAdapter (SpinnerAdapter adapter)
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
getDropDownView(int, View, ViewGroup)
.
Spinner overrides 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 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 |
Throws | |
---|---|
IllegalArgumentException |
if the adapter has more than one view type |
See also:
void setDropDownHorizontalOffset (int pixels)
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.
Related XML Attributes:
Parameters | |
---|---|
pixels |
int :
Horizontal offset in pixels |
void setDropDownVerticalOffset (int pixels)
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.
Related XML Attributes:
Parameters | |
---|---|
pixels |
int :
Vertical offset in pixels |
void setDropDownWidth (int pixels)
Set the width of the spinner's popup window of choices in pixels. This value
may also be set to MATCH_PARENT
to match the width of the Spinner itself, or
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.
Related XML Attributes:
Parameters | |
---|---|
pixels |
int :
Width in pixels, WRAP_CONTENT, or MATCH_PARENT |
void setEnabled (boolean enabled)
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.
|
void setGravity (int gravity)
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 |
int :
See Gravity |
void setOnItemClickListener (AdapterView.OnItemClickListener l)
A spinner does not support item click events. Calling this method will raise an exception.
Instead use setOnItemSelectedListener(AdapterView.OnItemSelectedListener)
.
Parameters | |
---|---|
l |
AdapterView.OnItemClickListener :
this listener will be ignored
|
void setPopupBackgroundDrawable (Drawable background)
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.
Related XML Attributes:
Parameters | |
---|---|
background |
Drawable :
Background drawable |
void setPopupBackgroundResource (int resId)
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.
Related XML Attributes:
Parameters | |
---|---|
resId |
int :
Resource ID of a background drawable |
void setPrompt (CharSequence prompt)
Sets the prompt to display when the dialog is shown.
Parameters | |
---|---|
prompt |
CharSequence :
the prompt to set
|
void setPromptId (int promptId)
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
|
void onDetachedFromWindow ()
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
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. Derived classes with children should override this method and call layout on each of their children.
Parameters | |
---|---|
changed |
boolean :
This is a new size or position for this view |
l |
int :
Left position, relative to parent |
t |
int :
Top position, relative to parent |
r |
int :
Right position, relative to parent |
b |
int :
Bottom position, relative to parent
|
See also:
void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
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
View.MeasureSpec . |
heightMeasureSpec |
int :
vertical space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |