Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

SwitchCompat

open class SwitchCompat : CompoundButton
kotlin.Any
   ↳ 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.

android.R.attr#textOn android.R.attr#textOff androidx.appcompat.R.attr#switchMinWidth androidx.appcompat.R.attr#switchPadding androidx.appcompat.R.attr#switchTextAppearance android.R.attr#thumb androidx.appcompat.R.attr#thumbTextPadding androidx.appcompat.R.attr#track

Summary

Public constructors
<init>(context: Context!)

Construct a new Switch with default styling.

<init>(context: Context!, attrs: AttributeSet!)

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

<init>(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

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

Public methods
open CharSequence!

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

open Boolean

open Unit

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.

open Unit

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

open Int

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

open CharSequence!

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

open ColorStateList?

open Unit
setTrackTintList(@Nullable tint: ColorStateList?)

Applies a tint to the track drawable.

open Unit
setThumbTintMode(@Nullable tintMode: Mode?)

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

open Mode?

open Unit
setTrackTintMode(@Nullable tintMode: Mode?)

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

open Boolean

Returns whether the track should be split by the thumb.

open Unit
setChecked(checked: Boolean)

open Boolean

open ColorStateList?

open Unit
setThumbTintList(@Nullable tint: ColorStateList?)

Applies a tint to the thumb drawable.

open Unit

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

open Unit

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

open Mode?

open Unit

open Unit

open Unit

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

open Unit

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

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Int

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

open Unit
setSwitchTextAppearance(context: Context!, resid: Int)

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

open Unit

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

open Unit

See TextViewCompat#setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

open Unit

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

open Drawable!

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

open Unit

open Unit
setShowText(showText: Boolean)

Sets whether the on/off text should be displayed.

open Drawable!

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

open Unit

open Unit

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

open Unit

Set the minimum width of the switch in pixels.

open Unit

open Int

open Unit

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

open Unit

open Unit
draw(c: Canvas!)

open Int

Get the minimum width of the switch in pixels.

open Unit
setSplitTrack(splitTrack: Boolean)

Specifies whether the track should be split by the thumb.

open Int

Protected methods
open Unit

open IntArray!

open Boolean

open Unit
onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)

open Unit
onDraw(canvas: Canvas!)

Public constructors

<init>

SwitchCompat(context: Context!)

Construct a new Switch with default styling.

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

<init>

SwitchCompat(context: Context!, attrs: AttributeSet!)

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 Context!: Specification of attributes that should deviate from default styling.

<init>

SwitchCompat(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

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 Context!: Specification of attributes that should deviate from the default styling.
defStyleAttr Context!: 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

getTextOn

open fun getTextOn(): CharSequence!

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

onTouchEvent

open fun onTouchEvent(ev: MotionEvent!): Boolean

setSwitchTypeface

open fun setSwitchTypeface(tf: Typeface!, style: Int): Unit

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.

setSwitchTypeface

open fun setSwitchTypeface(typeface: Typeface!): Unit

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.

getThumbTextPadding

open fun getThumbTextPadding(): Int

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

Return
Int: Horizontal padding for switch thumb text in pixels androidx.appcompat.R.attr#thumbTextPadding

getTextOff

open fun getTextOff(): CharSequence!

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

getTrackTintList

@Nullable open fun getTrackTintList(): ColorStateList?
Return
ColorStateList?: the tint applied to the track drawable androidx.appcompat.R.attr#trackTint

setTrackTintList

open fun setTrackTintList(@Nullable tint: ColorStateList?): Unit

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 androidx.appcompat.R.attr#trackTint

setThumbTintMode

open fun setThumbTintMode(@Nullable tintMode: Mode?): Unit

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 Mode?: the blending mode used to apply the tint, may be null to clear tint androidx.appcompat.R.attr#thumbTintMode

getThumbTintMode

@Nullable open fun getThumbTintMode(): Mode?
Return
Mode?: the blending mode used to apply the tint to the thumb drawable androidx.appcompat.R.attr#thumbTintMode

setTrackTintMode

open fun setTrackTintMode(@Nullable tintMode: Mode?): Unit

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 Mode?: the blending mode used to apply the tint, may be null to clear tint androidx.appcompat.R.attr#trackTintMode

getSplitTrack

open fun getSplitTrack(): Boolean

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

setChecked

open fun setChecked(checked: Boolean): Unit

getShowText

open fun getShowText(): Boolean
Return
Boolean: whether the on/off text should be displayed androidx.appcompat.R.attr#showText

getThumbTintList

@Nullable open fun getThumbTintList(): ColorStateList?
Return
ColorStateList?: the tint applied to the thumb drawable androidx.appcompat.R.attr#thumbTint

setThumbTintList

open fun setThumbTintList(@Nullable tint: ColorStateList?): Unit

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 androidx.appcompat.R.attr#thumbTint

setThumbResource

open fun setThumbResource(resId: Int): Unit

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 android.R.attr#thumb

setTextOn

open fun setTextOn(textOn: CharSequence!): Unit

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

getTrackTintMode

@Nullable open fun getTrackTintMode(): Mode?
Return
Mode?: the blending mode used to apply the tint to the track drawable androidx.appcompat.R.attr#trackTintMode

onInitializeAccessibilityNodeInfo

open fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo!): Unit

drawableHotspotChanged

open fun drawableHotspotChanged(x: Float, y: Float): Unit

setThumbTextPadding

open fun setThumbTextPadding(pixels: Int): Unit

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

Parameters
pixels Int: Horizontal padding for switch thumb text in pixels androidx.appcompat.R.attr#thumbTextPadding

setTextOff

open fun setTextOff(textOff: CharSequence!): Unit

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

onMeasure

open fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

getSwitchPadding

open fun getSwitchPadding(): Int

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

Return
Int: Amount of padding in pixels androidx.appcompat.R.attr#switchPadding

setSwitchTextAppearance

open fun setSwitchTextAppearance(context: Context!, resid: Int): Unit

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

setSwitchPadding

open fun setSwitchPadding(pixels: Int): Unit

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

Parameters
pixels Int: Amount of padding in pixels androidx.appcompat.R.attr#switchPadding

setCustomSelectionActionModeCallback

open fun setCustomSelectionActionModeCallback(actionModeCallback: Callback!): Unit

See TextViewCompat#setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

setTrackDrawable

open fun setTrackDrawable(track: Drawable!): Unit

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

Parameters
track Drawable!: Track drawable androidx.appcompat.R.attr#track

getTrackDrawable

open fun getTrackDrawable(): Drawable!

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

Return
Drawable!: Track drawable androidx.appcompat.R.attr#track

onPopulateAccessibilityEvent

open fun onPopulateAccessibilityEvent(event: AccessibilityEvent!): Unit

setShowText

open fun setShowText(showText: Boolean): Unit

Sets whether the on/off text should be displayed.

Parameters
showText Boolean: true to display on/off text androidx.appcompat.R.attr#showText

getThumbDrawable

open fun getThumbDrawable(): Drawable!

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

Return
Drawable!: Thumb drawable android.R.attr#thumb

toggle

open fun toggle(): Unit

setThumbDrawable

open fun setThumbDrawable(thumb: Drawable!): Unit

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 android.R.attr#thumb

setSwitchMinWidth

open fun setSwitchMinWidth(pixels: Int): Unit

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 androidx.appcompat.R.attr#switchMinWidth

jumpDrawablesToCurrentState

open fun jumpDrawablesToCurrentState(): Unit

getCompoundPaddingRight

open fun getCompoundPaddingRight(): Int

setTrackResource

open fun setTrackResource(resId: Int): Unit

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

Parameters
resId Int: Resource ID of a track drawable androidx.appcompat.R.attr#track

onInitializeAccessibilityEvent

open fun onInitializeAccessibilityEvent(event: AccessibilityEvent!): Unit

draw

open fun draw(c: Canvas!): Unit

getSwitchMinWidth

open fun getSwitchMinWidth(): Int

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.

Return
Int: Minimum width of the switch in pixels androidx.appcompat.R.attr#switchMinWidth

setSplitTrack

open fun setSplitTrack(splitTrack: Boolean): Unit

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 androidx.appcompat.R.attr#splitTrack

getCompoundPaddingLeft

open fun getCompoundPaddingLeft(): Int

Protected methods

drawableStateChanged

protected open fun drawableStateChanged(): Unit

onCreateDrawableState

protected open fun onCreateDrawableState(extraSpace: Int): IntArray!

verifyDrawable

protected open fun verifyDrawable(who: Drawable!): Boolean

onLayout

protected open fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int): Unit

onDraw

protected open fun onDraw(canvas: Canvas!): Unit