Most visited

Recently visited

AppCompatSpinner

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


A Spinner which supports compatible features on older versions 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

Inherited XML attributes

From class android.widget.Spinner
From class android.widget.AbsSpinner
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.widget.Spinner
From class android.widget.AdapterView
From class android.view.ViewGroup
From class android.view.View

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.

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

AppCompatSpinner

AppCompatSpinner (Context context)

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

AppCompatSpinner

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

AppCompatSpinner

AppCompatSpinner (Context context, 
                AttributeSet attrs)

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

AppCompatSpinner

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

AppCompatSpinner

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

AppCompatSpinner

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

Public methods

getDropDownHorizontalOffset

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
int Horizontal offset in pixels

getDropDownVerticalOffset

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
int Vertical offset in pixels

getDropDownWidth

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
int Width in pixels, WRAP_CONTENT, or MATCH_PARENT

getPopupBackground

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
Drawable background Background drawable

getPopupContext

Context getPopupContext ()

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

getPrompt

CharSequence getPrompt ()

Returns
CharSequence The prompt to display when the dialog is shown

onTouchEvent

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 MotionEvent: The motion event.
Returns
boolean True if the event was handled, false otherwise.

performClick

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.

setAdapter

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

setBackgroundDrawable

void setBackgroundDrawable (Drawable background)

This method is deprecated.
use setBackground(Drawable) instead

Parameters
background Drawable

setBackgroundResource

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 int: The identifier of the resource.

setDropDownHorizontalOffset

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 int: Horizontal offset in pixels

setDropDownVerticalOffset

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 int: Vertical offset in pixels

setDropDownWidth

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 int: Width in pixels, WRAP_CONTENT, or MATCH_PARENT

setPopupBackgroundDrawable

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 Drawable: Background drawable

setPopupBackgroundResource

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 int: Resource ID of a background drawable

setPrompt

void setPrompt (CharSequence prompt)

Sets the prompt to display when the dialog is shown.

Parameters
prompt CharSequence: the prompt to set

Protected methods

drawableStateChanged

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.

onDetachedFromWindow

void onDetachedFromWindow ()

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

onMeasure

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.

Hooray!