Class Switch

All Implemented Interfaces:
Drawable.Callback, ViewTreeObserver.OnPreDrawListener, Checkable, Checkable2

public class Switch extends Button implements Checkable2
A Switch is a two-state toggle widget. Users can drag the switch "thumb" back and forth to select either of two options or simply tap the switch to toggle between options.
Since:
3.12
  • Constructor Details

    • Switch

      public Switch(Context context)
      Construct a new Switch with default styling.
      Parameters:
      context - The Context that will determine this widget's theming.
    • Switch

      public Switch(Context context, @Nullable AttributeSet attrs)
      Construct a new Switch with default styling, overriding specific style attributes as requested.
      Parameters:
      context - The Context that will determine this widget's theming.
      attrs - Specification of attributes that should deviate from default styling.
    • Switch

      public Switch(Context context, @Nullable AttributeSet attrs, @Nullable @AttrRes ResourceId defStyleAttr)
      Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested.
      Parameters:
      context - The Context that will determine this widget's theming.
      attrs - Specification of attributes that should deviate from the default styling.
      defStyleAttr - An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be null to not look for defaults.
    • Switch

      public Switch(Context context, @Nullable AttributeSet attrs, @Nullable @AttrRes ResourceId defStyleAttr, @Nullable @StyleRes ResourceId defStyleRes)
      Construct a new Switch with a default style determined by the given theme attribute or style resource, overriding specific style attributes as requested.
      Parameters:
      context - The Context that will determine this widget's theming.
      attrs - Specification of attributes that should deviate from the default styling.
      defStyleAttr - An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be null to not look for defaults.
      defStyleRes - 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 null to not look for defaults.
  • Method Details

    • setSwitchPadding

      public void setSwitchPadding(int pixels)
      Set the amount of horizontal padding between the switch and the associated text.
      Parameters:
      pixels - Amount of padding in pixels
    • getSwitchPadding

      public int getSwitchPadding()
      Get the amount of horizontal padding between the switch and the associated text.
      Returns:
      Amount of padding in pixels
    • setSwitchMinWidth

      public void setSwitchMinWidth(int pixels)
      Set the minimum width of the switch in pixels. The switch's width will be the maximum of this value and its measured width as determined by the switch drawables and text used.
      Parameters:
      pixels - Minimum width of the switch in pixels
    • getSwitchMinWidth

      public int getSwitchMinWidth()
      Get the minimum width of the switch in pixels. The switch's width will be the maximum of this value and its measured width as determined by the switch drawables and text used.
      Returns:
      Minimum width of the switch in pixels
    • setTrackDrawable

      public void setTrackDrawable(Drawable track)
      Set the drawable used for the track that the switch slides within.
      Parameters:
      track - Track drawable
    • getTrackDrawable

      public Drawable getTrackDrawable()
      Get the drawable used for the track that the switch slides within.
      Returns:
      Track drawable
    • setTrackTintList

      public void setTrackTintList(@Nullable ColorStateList tint)
      Applies a tint to the track drawable. Does not modify the current tint mode, which is BlendMode.SRC_IN by default.

      Subsequent calls to setTrackDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using Drawable.setTintList(ColorStateList).

      Parameters:
      tint - the tint to apply, may be null to clear tint
      See Also:
    • getTrackTintList

      @Nullable public ColorStateList getTrackTintList()
      Returns:
      the tint applied to the track drawable
      See Also:
    • setTrackTintBlendMode

      public void setTrackTintBlendMode(@Nullable BlendMode blendMode)
      Specifies the blending mode used to apply the tint specified by setTrackTintList(ColorStateList)} to the track drawable. The default mode is BlendMode.SRC_IN.
      Parameters:
      blendMode - the blending mode used to apply the tint, may be null to clear tint
      See Also:
    • getTrackTintBlendMode

      @Nullable public BlendMode getTrackTintBlendMode()
      Returns:
      the blending mode used to apply the tint to the track drawable
      See Also:
    • setThumbDrawable

      public void setThumbDrawable(Drawable thumb)
      Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
      Parameters:
      thumb - Thumb drawable
    • getThumbDrawable

      public Drawable getThumbDrawable()
      Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
      Returns:
      Thumb drawable
    • setThumbTintList

      public void setThumbTintList(@Nullable ColorStateList tint)
      Applies a tint to the thumb drawable. Does not modify the current tint mode, which is BlendMode.SRC_IN by default.

      Subsequent calls to setThumbDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using Drawable.setTintList(ColorStateList).

      Parameters:
      tint - the tint to apply, may be null to clear tint
      See Also:
    • getThumbTintList

      @Nullable public ColorStateList getThumbTintList()
      Returns:
      the tint applied to the thumb drawable
      See Also:
    • setThumbTintBlendMode

      public void setThumbTintBlendMode(@Nullable BlendMode blendMode)
      Specifies the blending mode used to apply the tint specified by setThumbTintList(ColorStateList)} to the thumb drawable. The default mode is BlendMode.SRC_IN.
      Parameters:
      blendMode - the blending mode used to apply the tint, may be null to clear tint
      See Also:
    • getThumbTintBlendMode

      @Nullable public BlendMode getThumbTintBlendMode()
      Returns:
      the blending mode used to apply the tint to the thumb drawable
      See Also:
    • setSplitTrack

      public void setSplitTrack(boolean splitTrack)
      Specifies whether the track should be split by the thumb. When true, the thumb's optical bounds will be clipped out of the track drawable, then the thumb will be drawn into the resulting gap.
      Parameters:
      splitTrack - Whether the track should be split by the thumb
    • getSplitTrack

      public boolean getSplitTrack()
      Returns whether the track should be split by the thumb.
    • getTextOn

      @Nullable public CharSequence getTextOn()
      Returns the text for when the button is in the checked state.
      Returns:
      The text.
    • setTextOn

      public void setTextOn(@Nullable CharSequence textOn)
      Sets the text for when the button is in the checked state. If not null, it overrides the text previously set by TextView.setText(java.lang.CharSequence).
      Parameters:
      textOn - The text.
    • getTextOff

      @Nullable public CharSequence getTextOff()
      Returns the text for when the button is not in the checked state.
      Returns:
      The text.
    • setTextOff

      public void setTextOff(@Nullable CharSequence textOff)
      Sets the text for when the button is not in the checked state. If not null, it overrides the text previously set by TextView.setText(java.lang.CharSequence).
      Parameters:
      textOff - The text.
    • onMeasure

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      Description copied from class: View
      Measure the view and its content to determine the measured width and the measured height. This method is invoked by View.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 View.setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by View.measure(int, int). Calling super.onMeasure() 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 the base one to provide better measurements of their content.

      Overrides:
      onMeasure in class TextView
      Parameters:
      widthMeasureSpec - width measure specification imposed by the parent MeasureSpec
      heightMeasureSpec - height measure specification imposed by the parent MeasureSpec
    • getThumbPosition

      @FloatRange(from=0.0, to=1.0) protected final float getThumbPosition()
      Returns:
      the current thumb position as a decimal value between 0 (off) and 1 (on).
    • toggle

      public void toggle()
      Description copied from interface: Checkable
      Change the checked state of the view to the inverse of its current state
      Specified by:
      toggle in interface Checkable
    • 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.
    • isChecked

      public final boolean isChecked()
      Description copied from interface: Checkable
      Get the current checked state of the view
      Specified by:
      isChecked in interface Checkable
      Returns:
      The current checked state of the view
    • setChecked

      public void setChecked(boolean checked)
      Description copied from interface: Checkable
      Change the checked state of the view
      Specified by:
      setChecked in interface Checkable
      Parameters:
      checked - The new checked state
    • setOnCheckedChangeListener

      public void setOnCheckedChangeListener(@Nullable Checkable.OnCheckedChangeListener listener)
      Register a callback to be invoked when the checked state of this button changes.
      Specified by:
      setOnCheckedChangeListener in interface Checkable2
      Parameters:
      listener - the callback to call on checked state change
    • onLayout

      protected void onLayout(boolean changed, int left, int top, int right, int bottom)
      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 TextView
      Parameters:
      changed - This is a new size or position for this view
      left - Left position, relative to parent
      top - Top position, relative to parent
      right - Right position, relative to parent
      bottom - Bottom position, relative to parent
    • draw

      public void draw(@NonNull Canvas c)
      Description copied from class: View
      Base method that directly draws this view and its background, foreground, overlay and all children to the given canvas. When implementing a view, override View.onDraw(Canvas) instead of this.

      This is not the entry point for the view system to draw.

      Overrides:
      draw in class View
      Parameters:
      c - the canvas to draw content
    • onDraw

      protected void onDraw(@NonNull Canvas canvas)
      Description copied from class: View
      Draw the content of this view, implement this to do your drawing.

      Note that (0, 0) will be the top left of the bounds, and (width, height) will be the bottom right of the bounds.

      Overrides:
      onDraw in class TextView
      Parameters:
      canvas - the canvas to draw content
    • getCompoundPaddingLeft

      public int getCompoundPaddingLeft()
      Description copied from class: TextView
      Returns the left padding of the view, plus space for the left Drawable if any.
      Overrides:
      getCompoundPaddingLeft in class TextView
    • getCompoundPaddingRight

      public int getCompoundPaddingRight()
      Description copied from class: TextView
      Returns the right padding of the view, plus space for the right Drawable if any.
      Overrides:
      getCompoundPaddingRight in class TextView
    • onCreateDrawableState

      @NonNull protected int[] onCreateDrawableState(int extraSpace)
      Description copied from class: View
      Generate the new Drawable state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use View.getDrawableState().
      Overrides:
      onCreateDrawableState in class TextView
      Parameters:
      extraSpace - if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states.
      Returns:
      an array holding the current Drawable state of the view.
      See Also:
    • drawableStateChanged

      protected void drawableStateChanged()
      Description copied from class: View
      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.

      Overrides:
      drawableStateChanged in class TextView
      See Also:
    • drawableHotspotChanged

      public void drawableHotspotChanged(float x, float y)
      Description copied from class: View
      This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.

      Dispatching to child views is handled by View.dispatchDrawableHotspotChanged(float, float).

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

      Overrides:
      drawableHotspotChanged in class TextView
      Parameters:
      x - hotspot x coordinate
      y - hotspot y coordinate
    • verifyDrawable

      protected boolean verifyDrawable(@NonNull Drawable who)
      Description copied from class: View
      If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.

      Be sure to call through to the super class when overriding this function.

      Overrides:
      verifyDrawable in class TextView
      Parameters:
      who - The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.
      Returns:
      boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate.
      See Also:
    • jumpDrawablesToCurrentState

      public void jumpDrawablesToCurrentState()
      Description copied from class: View
      Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

      Also calls StateListAnimator.jumpToCurrentState() if there is a StateListAnimator attached to this view.

      Overrides:
      jumpDrawablesToCurrentState in class TextView