Class Spinner

All Implemented Interfaces:
Drawable.Callback, ViewManager, ViewParent

public class Spinner extends AbsSpinner
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.

  • Constructor Details

    • Spinner

      public Spinner(Context context)
  • Method Details

    • setPopupBackgroundDrawable

      public void setPopupBackgroundDrawable(@Nullable Drawable background)
      Set the background drawable for the spinner's popup window of choices.
      Parameters:
      background - Background drawable
    • getPopupBackground

      @Nullable public Drawable getPopupBackground()
      Get the background drawable for the spinner's popup window of choices.
      Returns:
      Background drawable
    • setDropDownVerticalOffset

      public void setDropDownVerticalOffset(int pixels)
      Set a vertical offset in pixels for the spinner's popup window of choices.
      Parameters:
      pixels - Vertical offset in pixels
    • getDropDownVerticalOffset

      public int getDropDownVerticalOffset()
      Get the configured vertical offset in pixels for the spinner's popup window of choices.
      Returns:
      Vertical offset in pixels
    • setDropDownHorizontalOffset

      public void setDropDownHorizontalOffset(int pixels)
      Set a horizontal offset in pixels for the spinner's popup window of choices.
      Parameters:
      pixels - Horizontal offset in pixels
    • getDropDownHorizontalOffset

      public int getDropDownHorizontalOffset()
      Get the configured horizontal offset in pixels for the spinner's popup window of choices.
      Returns:
      Horizontal offset in pixels
    • setDropDownWidth

      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 ViewGroup.LayoutParams.MATCH_PARENT to match the width of the Spinner itself, or ViewGroup.LayoutParams.WRAP_CONTENT to wrap to the measured size of contained dropdown list items.
      Parameters:
      pixels - Width in pixels, WRAP_CONTENT, or MATCH_PARENT
    • getDropDownWidth

      public int getDropDownWidth()
      Get the configured width of the spinner's popup window of choices in pixels. The returned value may also be ViewGroup.LayoutParams.MATCH_PARENT meaning the popup window will match the width of the Spinner itself, or ViewGroup.LayoutParams.WRAP_CONTENT to wrap to the measured size of contained dropdown list items.
      Returns:
      Width in pixels, WRAP_CONTENT, or MATCH_PARENT
    • setEnabled

      public void setEnabled(boolean enabled)
      Description copied from class: View
      Set the enabled state of this view. The interpretation of the enabled state varies by subclass.
      Overrides:
      setEnabled in class View
      Parameters:
      enabled - True if this view is enabled, false otherwise.
    • setGravity

      public void setGravity(int gravity)
      Describes how the selected item view is positioned. Currently, only the horizontal component is used. The default is Gravity.START.
      Parameters:
      gravity - See Gravity
    • getGravity

      public int getGravity()
      Describes how the selected item view is positioned. The default is Gravity.START.
      Returns:
      A Gravity value
    • setAdapter

      public void setAdapter(@Nullable SpinnerAdapter adapter)
      Sets the SpinnerAdapter used to provide the data which backs this Spinner.

      Spinner overrides Adapter.getViewTypeCount() on the Adapter associated with this view. Calling getItemViewType(int) on the object returned from AbsSpinner.getAdapter() will always return 0. Calling getViewTypeCount() will always return 1. Attempting to set an adapter with more than one view type will throw an IllegalArgumentException.

      Overrides:
      setAdapter in class AbsSpinner
      Parameters:
      adapter - the adapter to set
      Throws:
      IllegalArgumentException - if the adapter has more than one view type
      See Also:
    • getBaseline

      public int getBaseline()
      Description copied from class: View

      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.

      Overrides:
      getBaseline in class View
      Returns:
      the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported
    • onDetachedFromWindow

      protected void onDetachedFromWindow()
      Description copied from class: View
      This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
      Overrides:
      onDetachedFromWindow in class AdapterView<SpinnerAdapter>
      See Also:
    • setOnItemClickListener

      public void setOnItemClickListener(@Nullable AdapterView.OnItemClickListener l)

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

      Instead use AdapterView.setOnItemSelectedListener(icyllis.modernui.widget.AdapterView.OnItemSelectedListener).

      Overrides:
      setOnItemClickListener in class AdapterView<SpinnerAdapter>
      Parameters:
      l - this listener will be ignored
    • onTouchEvent

      public boolean onTouchEvent(@Nonnull MotionEvent event)
      Description copied from class: View
      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 View.performClick(). This will ensure consistent system behavior.

      Overrides:
      onTouchEvent in class View
      Parameters:
      event - the touch event
      Returns:
      true if the event was handled by the view, false otherwise
    • onMeasure

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      Description copied from class: AbsSpinner
      Figure out the dimensions of this Spinner. The width comes from the widthMeasureSpec as Spinners can't have their width set to UNSPECIFIED. The height is based on the height of the selected item plus padding.
      Overrides:
      onMeasure in class AbsSpinner
      Parameters:
      widthMeasureSpec - width measure specification imposed by the parent MeasureSpec
      heightMeasureSpec - height measure specification imposed by the parent MeasureSpec
      See Also:
    • onLayout

      protected void onLayout(boolean changed, int l, int t, int r, int b)
      Description copied from class: View
      Called from View.layout(int, int, int, int) 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.

      Overrides:
      onLayout in class AdapterView<SpinnerAdapter>
      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
      See Also:
    • performClick

      public boolean performClick()
      Description copied from class: View
      Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.
      Overrides:
      performClick in class View
      Returns:
      True there was an assigned OnClickListener that was called, false otherwise is returned.
    • onResolvePointerIcon

      public PointerIcon onResolvePointerIcon(@Nonnull MotionEvent event)
      Description copied from class: View
      Returns the pointer icon for the motion event, or null if it doesn't specify the icon. The default implementation does not care the location or event types, but some subclasses may use it (such as WebViews).
      Overrides:
      onResolvePointerIcon in class ViewGroup
      Parameters:
      event - The MotionEvent from a mouse
      See Also: