Register now for Android Dev Summit 2019!

SwitchCompat

public class SwitchCompat
extends CompoundButton

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
         ↳ android.widget.CompoundButton
           ↳ androidx.appcompat.widget.SwitchCompat


SwitchCompat is a version of the Switch widget which on devices back to API v7. It does not make any attempt to use the platform provided widget on those devices which it is available normally.

A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle as if it were a checkbox. The text property controls the text displayed in the label for the switch, whereas the off and on text controls the text on the thumb. Similarly, the textAppearance and the related setTypeface() methods control the typeface and style of label text, whereas the switchTextAppearance and the related setSwitchTypeface() methods control that of the thumb.

See the Toggle Buttons guide.

R.attr.textOn R.attr.textOff R.attr.switchMinWidth R.attr.switchPadding R.attr.switchTextAppearance R.attr.thumb R.attr.thumbTextPadding R.attr.track

Summary

Inherited constants

Inherited fields

Public constructors

SwitchCompat(Context context)

Construct a new Switch with default styling.

SwitchCompat(Context context, AttributeSet attrs)

Construct a new Switch with default styling, overriding specific style attributes as requested.

SwitchCompat(Context context, AttributeSet attrs, int defStyleAttr)

Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested.

Public methods

void draw(Canvas c)
void drawableHotspotChanged(float x, float y)
int getCompoundPaddingLeft()
int getCompoundPaddingRight()
boolean getShowText()
boolean getSplitTrack()

Returns whether the track should be split by the thumb.

int getSwitchMinWidth()

Get the minimum width of the switch in pixels.

int getSwitchPadding()

Get the amount of horizontal padding between the switch and the associated text.

CharSequence getTextOff()

Returns the text displayed when the button is not in the checked state.

CharSequence getTextOn()

Returns the text displayed when the button is in the checked state.

Drawable getThumbDrawable()

Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.

int getThumbTextPadding()

Get the horizontal padding around the text drawn on the switch itself.

ColorStateList getThumbTintList()
PorterDuff.Mode getThumbTintMode()
Drawable getTrackDrawable()

Get the drawable used for the track that the switch slides within.

ColorStateList getTrackTintList()
PorterDuff.Mode getTrackTintMode()
void jumpDrawablesToCurrentState()
void onInitializeAccessibilityEvent(AccessibilityEvent event)
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onPopulateAccessibilityEvent(AccessibilityEvent event)
boolean onTouchEvent(MotionEvent ev)
void setChecked(boolean checked)
void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback)

See TextViewCompat.setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

void setShowText(boolean showText)

Sets whether the on/off text should be displayed.

void setSplitTrack(boolean splitTrack)

Specifies whether the track should be split by the thumb.

void setSwitchMinWidth(int pixels)

Set the minimum width of the switch in pixels.

void setSwitchPadding(int pixels)

Set the amount of horizontal padding between the switch and the associated text.

void setSwitchTextAppearance(Context context, int resid)

Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

void setSwitchTypeface(Typeface typeface)

Sets the typeface in which the text should be displayed on the switch.

void setSwitchTypeface(Typeface tf, int style)

Sets the typeface and style in which the text should be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified.

void setTextOff(CharSequence textOff)

Sets the text displayed when the button is not in the checked state.

void setTextOn(CharSequence textOn)

Sets the text displayed when the button is in the checked state.

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.

void setThumbResource(int resId)

Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.

void setThumbTextPadding(int pixels)

Set the horizontal padding around the text drawn on the switch itself.

void setThumbTintList(ColorStateList tint)

Applies a tint to the thumb drawable.

void setThumbTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setThumbTintList(ColorStateList)} to the thumb drawable.

void setTrackDrawable(Drawable track)

Set the drawable used for the track that the switch slides within.

void setTrackResource(int resId)

Set the drawable used for the track that the switch slides within.

void setTrackTintList(ColorStateList tint)

Applies a tint to the track drawable.

void setTrackTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setTrackTintList(ColorStateList)} to the track drawable.

void toggle()

Protected methods

void drawableStateChanged()
int[] onCreateDrawableState(int extraSpace)
void onDraw(Canvas canvas)
void onLayout(boolean changed, int left, int top, int right, int bottom)
boolean verifyDrawable(Drawable who)

Inherited methods

Public constructors

SwitchCompat

public SwitchCompat (Context context)

Construct a new Switch with default styling.

Parameters
context Context: The Context that will determine this widget's theming.

SwitchCompat

public SwitchCompat (Context context, 
                AttributeSet attrs)

Construct a new Switch with default styling, overriding specific style attributes as requested.

Parameters
context Context: The Context that will determine this widget's theming.

attrs AttributeSet: Specification of attributes that should deviate from default styling.

SwitchCompat

public SwitchCompat (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Construct a new Switch with a default style determined by the given theme attribute, overriding specific style attributes as requested.

Parameters
context Context: The Context that will determine this widget's theming.

attrs AttributeSet: Specification of attributes that should deviate from the default styling.

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.

Public methods

draw

public void draw (Canvas c)

Parameters
c Canvas

drawableHotspotChanged

public void drawableHotspotChanged (float x, 
                float y)

Parameters
x float

y float

getCompoundPaddingLeft

public int getCompoundPaddingLeft ()

Returns
int

getCompoundPaddingRight

public int getCompoundPaddingRight ()

Returns
int

getShowText

public boolean getShowText ()

Returns
boolean whether the on/off text should be displayed R.attr.showText

getSplitTrack

public boolean getSplitTrack ()

Returns whether the track should be split by the thumb. R.attr.splitTrack

Returns
boolean

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
int Minimum width of the switch in pixels R.attr.switchMinWidth

getSwitchPadding

public int getSwitchPadding ()

Get the amount of horizontal padding between the switch and the associated text.

Returns
int Amount of padding in pixels R.attr.switchPadding

getTextOff

public CharSequence getTextOff ()

Returns the text displayed when the button is not in the checked state. R.attr.textOff

Returns
CharSequence

getTextOn

public CharSequence getTextOn ()

Returns the text displayed when the button is in the checked state. R.attr.textOn

Returns
CharSequence

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
Drawable Thumb drawable R.attr.thumb

getThumbTextPadding

public int getThumbTextPadding ()

Get the horizontal padding around the text drawn on the switch itself.

Returns
int Horizontal padding for switch thumb text in pixels R.attr.thumbTextPadding

getThumbTintList

public ColorStateList getThumbTintList ()

Returns
ColorStateList the tint applied to the thumb drawable R.attr.thumbTint

getThumbTintMode

public PorterDuff.Mode getThumbTintMode ()

Returns
PorterDuff.Mode the blending mode used to apply the tint to the thumb drawable R.attr.thumbTintMode

getTrackDrawable

public Drawable getTrackDrawable ()

Get the drawable used for the track that the switch slides within.

Returns
Drawable Track drawable R.attr.track

getTrackTintList

public ColorStateList getTrackTintList ()

Returns
ColorStateList the tint applied to the track drawable R.attr.trackTint

getTrackTintMode

public PorterDuff.Mode getTrackTintMode ()

Returns
PorterDuff.Mode the blending mode used to apply the tint to the track drawable R.attr.trackTintMode

jumpDrawablesToCurrentState

public void jumpDrawablesToCurrentState ()

onInitializeAccessibilityEvent

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Parameters
event AccessibilityEvent

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Parameters
info AccessibilityNodeInfo

onMeasure

public void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onPopulateAccessibilityEvent

public void onPopulateAccessibilityEvent (AccessibilityEvent event)

Parameters
event AccessibilityEvent

onTouchEvent

public boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

setChecked

public void setChecked (boolean checked)

Parameters
checked boolean

setCustomSelectionActionModeCallback

public void setCustomSelectionActionModeCallback (ActionMode.Callback actionModeCallback)

See TextViewCompat.setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

Parameters
actionModeCallback ActionMode.Callback

setShowText

public void setShowText (boolean showText)

Sets whether the on/off text should be displayed.

Parameters
showText boolean: true to display on/off text R.attr.showText

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 boolean: Whether the track should be split by the thumb R.attr.splitTrack

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 int: Minimum width of the switch in pixels R.attr.switchMinWidth

setSwitchPadding

public void setSwitchPadding (int pixels)

Set the amount of horizontal padding between the switch and the associated text.

Parameters
pixels int: Amount of padding in pixels R.attr.switchPadding

setSwitchTextAppearance

public void setSwitchTextAppearance (Context context, 
                int resid)

Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource. R.attr.switchTextAppearance

Parameters
context Context

resid int

setSwitchTypeface

public void setSwitchTypeface (Typeface typeface)

Sets the typeface in which the text should be displayed on the switch. Note that not all Typeface families actually have bold and italic variants, so you may need to use setSwitchTypeface(Typeface, int) to get the appearance that you actually want.

Parameters
typeface Typeface

setSwitchTypeface

public void setSwitchTypeface (Typeface tf, 
                int style)

Sets the typeface and style in which the text should be displayed on the switch, and turns on the fake bold and italic bits in the Paint if the Typeface that you provided does not have all the bits in the style that you specified.

Parameters
tf Typeface

style int

setTextOff

public void setTextOff (CharSequence textOff)

Sets the text displayed when the button is not in the checked state. R.attr.textOff

Parameters
textOff CharSequence

setTextOn

public void setTextOn (CharSequence textOn)

Sets the text displayed when the button is in the checked state. R.attr.textOn

Parameters
textOn CharSequence

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 Drawable: Thumb drawable R.attr.thumb

setThumbResource

public void setThumbResource (int resId)

Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.

Parameters
resId int: Resource ID of a thumb drawable R.attr.thumb

setThumbTextPadding

public void setThumbTextPadding (int pixels)

Set the horizontal padding around the text drawn on the switch itself.

Parameters
pixels int: Horizontal padding for switch thumb text in pixels R.attr.thumbTextPadding

setThumbTintList

public void setThumbTintList (ColorStateList tint)

Applies a tint to the thumb drawable. Does not modify the current tint mode, which is PorterDuff.Mode.SRC_IN by default.

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

Parameters
tint ColorStateList: the tint to apply, may be null to clear tint R.attr.thumbTint

setThumbTintMode

public void setThumbTintMode (PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setThumbTintList(ColorStateList)} to the thumb drawable. The default mode is PorterDuff.Mode.SRC_IN.

Parameters
tintMode PorterDuff.Mode: the blending mode used to apply the tint, may be null to clear tint R.attr.thumbTintMode

setTrackDrawable

public void setTrackDrawable (Drawable track)

Set the drawable used for the track that the switch slides within.

Parameters
track Drawable: Track drawable R.attr.track

setTrackResource

public void setTrackResource (int resId)

Set the drawable used for the track that the switch slides within.

Parameters
resId int: Resource ID of a track drawable R.attr.track

setTrackTintList

public void setTrackTintList (ColorStateList tint)

Applies a tint to the track drawable. Does not modify the current tint mode, which is PorterDuff.Mode.SRC_IN by default.

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

Parameters
tint ColorStateList: the tint to apply, may be null to clear tint R.attr.trackTint

See also:

setTrackTintMode

public void setTrackTintMode (PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setTrackTintList(ColorStateList)} to the track drawable. The default mode is PorterDuff.Mode.SRC_IN.

Parameters
tintMode PorterDuff.Mode: the blending mode used to apply the tint, may be null to clear tint R.attr.trackTintMode

See also:

toggle

public void toggle ()

Protected methods

drawableStateChanged

protected void drawableStateChanged ()

onCreateDrawableState

protected int[] onCreateDrawableState (int extraSpace)

Parameters
extraSpace int

Returns
int[]

onDraw

protected void onDraw (Canvas canvas)

Parameters
canvas Canvas

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int

verifyDrawable

protected boolean verifyDrawable (Drawable who)

Parameters
who Drawable

Returns
boolean