Skip to content

Most visited

Recently visited

navigation
added in version 25.1.0
belongs to Maven artifact com.android.support:appcompat-v7:27.0.0

SwitchCompat

public class SwitchCompat
extends CompoundButton

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
         ↳ android.widget.CompoundButton
           ↳ android.support.v7.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.

Summary

XML attributes

SwitchCompat_android_textOff  
SwitchCompat_android_textOn  
SwitchCompat_android_thumb  
SwitchCompat_showText  
SwitchCompat_splitTrack  
SwitchCompat_switchMinWidth  
SwitchCompat_switchPadding  
SwitchCompat_switchTextAppearance  
SwitchCompat_thumbTextPadding  
SwitchCompat_thumbTint  
SwitchCompat_thumbTintMode  
SwitchCompat_track  
SwitchCompat_trackTint  
SwitchCompat_trackTintMode  

Inherited constants

From class android.widget.TextView
From class android.view.View

Inherited fields

From class android.view.View

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

From class android.widget.CompoundButton
From class android.widget.Button
From class android.widget.TextView
From class android.view.View
From class java.lang.Object
From interface android.widget.Checkable
From interface android.view.ViewTreeObserver.OnPreDrawListener
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML attributes

SwitchCompat_android_textOff

Related methods:

SwitchCompat_android_textOn

Related methods:

SwitchCompat_android_thumb

Related methods:

SwitchCompat_showText

Related methods:

SwitchCompat_splitTrack

Related methods:

SwitchCompat_switchMinWidth

Related methods:

SwitchCompat_switchPadding

Related methods:

SwitchCompat_switchTextAppearance

Related methods:

SwitchCompat_thumbTextPadding

Related methods:

SwitchCompat_thumbTint

Related methods:

SwitchCompat_thumbTintMode

Related methods:

SwitchCompat_track

Related methods:

SwitchCompat_trackTint

Related methods:

SwitchCompat_trackTintMode

Related methods:

Public constructors

SwitchCompat

added in version 25.1.0
SwitchCompat (Context context)

Construct a new Switch with default styling.

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

SwitchCompat

added in version 25.1.0
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

added in version 25.1.0
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

void draw (Canvas c)

Parameters
c Canvas

drawableHotspotChanged

void drawableHotspotChanged (float x, 
                float y)

Parameters
x float

y float

getCompoundPaddingLeft

int getCompoundPaddingLeft ()

Returns
int

getCompoundPaddingRight

int getCompoundPaddingRight ()

Returns
int

getShowText

added in version 25.1.0
boolean getShowText ()

Related XML Attributes:

Returns
boolean whether the on/off text should be displayed

getSplitTrack

added in version 25.1.0
boolean getSplitTrack ()

Returns whether the track should be split by the thumb.

Related XML Attributes:

Returns
boolean

getSwitchMinWidth

added in version 25.1.0
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.

Related XML Attributes:

Returns
int Minimum width of the switch in pixels

getSwitchPadding

added in version 25.1.0
int getSwitchPadding ()

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

Related XML Attributes:

Returns
int Amount of padding in pixels

getTextOff

added in version 25.1.0
CharSequence getTextOff ()

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

Related XML Attributes:

Returns
CharSequence

getTextOn

added in version 25.1.0
CharSequence getTextOn ()

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

Related XML Attributes:

Returns
CharSequence

getThumbDrawable

added in version 25.1.0
Drawable getThumbDrawable ()

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

Related XML Attributes:

Returns
Drawable Thumb drawable

getThumbTextPadding

added in version 25.1.0
int getThumbTextPadding ()

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

Related XML Attributes:

Returns
int Horizontal padding for switch thumb text in pixels

getThumbTintList

added in version 25.1.0
ColorStateList getThumbTintList ()

Related XML Attributes:

Returns
ColorStateList the tint applied to the thumb drawable

See also:

getThumbTintMode

added in version 25.1.0
PorterDuff.Mode getThumbTintMode ()

Related XML Attributes:

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

See also:

getTrackDrawable

added in version 25.1.0
Drawable getTrackDrawable ()

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

Related XML Attributes:

Returns
Drawable Track drawable

getTrackTintList

added in version 25.1.0
ColorStateList getTrackTintList ()

Related XML Attributes:

Returns
ColorStateList the tint applied to the track drawable

See also:

getTrackTintMode

added in version 25.1.0
PorterDuff.Mode getTrackTintMode ()

Related XML Attributes:

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

See also:

jumpDrawablesToCurrentState

void jumpDrawablesToCurrentState ()

onInitializeAccessibilityEvent

void onInitializeAccessibilityEvent (AccessibilityEvent event)

Parameters
event AccessibilityEvent

onInitializeAccessibilityNodeInfo

void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Parameters
info AccessibilityNodeInfo

onMeasure

added in version 25.1.0
void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onPopulateAccessibilityEvent

void onPopulateAccessibilityEvent (AccessibilityEvent event)

Parameters
event AccessibilityEvent

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

setChecked

void setChecked (boolean checked)

Parameters
checked boolean

setShowText

added in version 25.1.0
void setShowText (boolean showText)

Sets whether the on/off text should be displayed.

Related XML Attributes:

Parameters
showText boolean: true to display on/off text

setSplitTrack

added in version 25.1.0
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.

Related XML Attributes:

Parameters
splitTrack boolean: Whether the track should be split by the thumb

setSwitchMinWidth

added in version 25.1.0
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.

Related XML Attributes:

Parameters
pixels int: Minimum width of the switch in pixels

setSwitchPadding

added in version 25.1.0
void setSwitchPadding (int pixels)

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

Related XML Attributes:

Parameters
pixels int: Amount of padding in pixels

setSwitchTextAppearance

added in version 25.1.0
void setSwitchTextAppearance (Context context, 
                int resid)

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

Related XML Attributes:

Parameters
context Context

resid int

setSwitchTypeface

added in version 25.1.0
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

added in version 25.1.0
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

added in version 25.1.0
void setTextOff (CharSequence textOff)

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

Related XML Attributes:

Parameters
textOff CharSequence

setTextOn

added in version 25.1.0
void setTextOn (CharSequence textOn)

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

Related XML Attributes:

Parameters
textOn CharSequence

setThumbDrawable

added in version 25.1.0
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.

Related XML Attributes:

Parameters
thumb Drawable: Thumb drawable

setThumbResource

added in version 25.1.0
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.

Related XML Attributes:

Parameters
resId int: Resource ID of a thumb drawable

setThumbTextPadding

added in version 25.1.0
void setThumbTextPadding (int pixels)

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

Related XML Attributes:

Parameters
pixels int: Horizontal padding for switch thumb text in pixels

setThumbTintList

added in version 25.1.0
void setThumbTintList (ColorStateList tint)

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

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

Related XML Attributes:

Parameters
tint ColorStateList: the tint to apply, may be null to clear tint

See also:

setThumbTintMode

added in version 25.1.0
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 SRC_IN.

Related XML Attributes:

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

See also:

setTrackDrawable

added in version 25.1.0
void setTrackDrawable (Drawable track)

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

Related XML Attributes:

Parameters
track Drawable: Track drawable

setTrackResource

added in version 25.1.0
void setTrackResource (int resId)

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

Related XML Attributes:

Parameters
resId int: Resource ID of a track drawable

setTrackTintList

added in version 25.1.0
void setTrackTintList (ColorStateList tint)

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

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

Related XML Attributes:

Parameters
tint ColorStateList: the tint to apply, may be null to clear tint

See also:

setTrackTintMode

added in version 25.1.0
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 SRC_IN.

Related XML Attributes:

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

See also:

toggle

void toggle ()

Protected methods

drawableStateChanged

void drawableStateChanged ()

onCreateDrawableState

int[] onCreateDrawableState (int extraSpace)

Parameters
extraSpace int

Returns
int[]

onDraw

void onDraw (Canvas canvas)

Parameters
canvas Canvas

onLayout

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

Parameters
changed boolean

left int

top int

right int

bottom int

verifyDrawable

boolean verifyDrawable (Drawable who)

Parameters
who Drawable

Returns
boolean

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)