Android APIs
public class

AppCompatSpinner

extends Spinner
implements TintableBackgroundView
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.AdapterView<android.widget.SpinnerAdapter>
         ↳ android.widget.AbsSpinner
           ↳ android.widget.Spinner
             ↳ android.support.v7.widget.AppCompatSpinner

Class Overview

A Spinner which supports compatible features on older version of the platform, including:

This will automatically be used when you use Spinner in your layouts. You should only need to manually use this class when writing custom views.

Summary

[Expand]
Inherited XML Attributes
From class android.widget.Spinner
From class android.widget.AbsSpinner
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.widget.Spinner
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
AppCompatSpinner(Context context)
Construct a new spinner with the given context's theme.
AppCompatSpinner(Context context, int mode)
Construct a new spinner with the given context's theme and the supplied mode of displaying choices.
AppCompatSpinner(Context context, AttributeSet attrs)
Construct a new spinner with the given context's theme and the supplied attribute set.
AppCompatSpinner(Context context, AttributeSet attrs, int defStyleAttr)
Construct a new spinner with the given context's theme, the supplied attribute set, and default style attribute.
AppCompatSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode)
Construct a new spinner with the given context's theme, the supplied attribute set, and default style.
AppCompatSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme)
Constructs a new spinner with the given context's theme, the supplied attribute set, default styles, popup mode (one of MODE_DIALOG or MODE_DROPDOWN), and the context against which the popup should be inflated.
Public Methods
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.
Drawable getPopupBackground()
Get the background drawable for the spinner's popup window of choices.
Context getPopupContext()
CharSequence getPrompt()
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 SpinnerAdapter used to provide the data which backs this Spinner.
void setBackgroundDrawable(Drawable background)
This method is deprecated. use setBackground(Drawable) instead
void setBackgroundResource(int resId)
Set the background to a given resource.
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 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.
Protected Methods
void drawableStateChanged()
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
void onDetachedFromWindow()
This is called when the view is detached from a window.
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.Spinner
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.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.support.v4.view.TintableBackgroundView

Public Constructors

public AppCompatSpinner (Context context)

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 AppCompatSpinner (Context context, int mode)

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 AppCompatSpinner (Context context, AttributeSet attrs)

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 AppCompatSpinner (Context context, AttributeSet attrs, int defStyleAttr)

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

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

public AppCompatSpinner (Context context, AttributeSet attrs, int defStyleAttr, int mode)

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.
defStyleAttr 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 Constant describing how the user will select choices from the spinner.

public AppCompatSpinner (Context context, AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme)

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

Parameters
context The context against which the view is inflated, which provides access to the current theme, resources, etc.
attrs The attributes of the XML tag that is inflating the view.
defStyleAttr 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 Constant describing how the user will select choices from the spinner.
popupTheme 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.

Public Methods

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

Returns
  • Horizontal offset in pixels

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

Returns
  • Vertical offset in pixels

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

Returns
  • Width in pixels, WRAP_CONTENT, or MATCH_PARENT

public Drawable getPopupBackground ()

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

Returns
  • background Background drawable

public Context getPopupContext ()

Returns
  • the context used to inflate the Spinner's popup or dialog window

public CharSequence getPrompt ()

Returns
  • The prompt to display when the dialog is shown

public 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:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

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

public 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
  • True there was an assigned OnClickListener that was called, false otherwise is returned.

public 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 the adapter to set

public void setBackgroundDrawable (Drawable background)

This method is deprecated.
use setBackground(Drawable) instead

public void setBackgroundResource (int resId)

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

Parameters
resId The identifier of the resource.

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

Parameters
pixels Horizontal offset in pixels

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

Parameters
pixels Vertical offset in pixels

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

Parameters
pixels Width in pixels, WRAP_CONTENT, or MATCH_PARENT

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

Parameters
background Background drawable

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

Parameters
resId Resource ID of a background drawable

public void setPrompt (CharSequence prompt)

Sets the prompt to display when the dialog is shown.

Parameters
prompt the prompt to set

Protected Methods

protected void drawableStateChanged ()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

If the View has a StateListAnimator, it will also be called to run necessary state change animations.

Be sure to call through to the superclass when overriding this function.

protected void onDetachedFromWindow ()

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

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