The Android Developer Challenge is back! Submit your idea before December 2.

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 Unit
draw(c: Canvas!)

open Unit

open Int

open Int

open Boolean

open Boolean

Returns whether the track should be split by the thumb.

open Int

Get the minimum width of the switch in pixels.

open Int

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

open CharSequence!

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

open CharSequence!

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

open Drawable!

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

open Int

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

open ColorStateList?

open Mode?

open Drawable!

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

open ColorStateList?

open Mode?

open Unit

open Unit

open Unit

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

open Unit

open Boolean

open Unit
setChecked(checked: Boolean)

open Unit

See TextViewCompat#setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

open Unit
setShowText(showText: Boolean)

Sets whether the on/off text should be displayed.

open Unit
setSplitTrack(splitTrack: Boolean)

Specifies whether the track should be split by the thumb.

open Unit

Set the minimum width of the switch in pixels.

open Unit

Set 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

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 Unit

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

open Unit

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

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 drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.

open Unit

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

open Unit
setThumbTintList(@Nullable tint: ColorStateList?)

Applies a tint to the thumb 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 Unit

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

open Unit

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

open Unit
setTrackTintList(@Nullable tint: ColorStateList?)

Applies a tint to the track drawable.

open Unit
setTrackTintMode(@Nullable tintMode: Mode?)

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

open Unit

Protected methods

open Unit

open IntArray!

open Unit
onDraw(canvas: Canvas!)

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

open Boolean

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

draw

open fun draw(c: Canvas!): Unit

drawableHotspotChanged

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

getCompoundPaddingLeft

open fun getCompoundPaddingLeft(): Int

getCompoundPaddingRight

open fun getCompoundPaddingRight(): Int

getShowText

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

getSplitTrack

open fun getSplitTrack(): Boolean

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

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

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

getTextOff

open fun getTextOff(): CharSequence!

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

getTextOn

open fun getTextOn(): CharSequence!

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

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

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

getThumbTintList

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

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

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

getTrackTintList

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

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

jumpDrawablesToCurrentState

open fun jumpDrawablesToCurrentState(): Unit

onInitializeAccessibilityEvent

open fun onInitializeAccessibilityEvent(event: AccessibilityEvent!): Unit

onInitializeAccessibilityNodeInfo

open fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo!): Unit

onMeasure

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

onPopulateAccessibilityEvent

open fun onPopulateAccessibilityEvent(event: AccessibilityEvent!): Unit

onTouchEvent

open fun onTouchEvent(ev: MotionEvent!): Boolean

setChecked

open fun setChecked(checked: Boolean): Unit

setCustomSelectionActionModeCallback

open fun setCustomSelectionActionModeCallback(actionModeCallback: Callback!): Unit

See TextViewCompat#setCustomSelectionActionModeCallback(TextView, ActionMode.Callback)

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

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

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

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

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

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.

setTextOff

open fun setTextOff(textOff: CharSequence!): Unit

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

setTextOn

open fun setTextOn(textOn: CharSequence!): Unit

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

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

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

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

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

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

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

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

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

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

toggle

open fun toggle(): Unit

Protected methods

drawableStateChanged

protected open fun drawableStateChanged(): Unit

onCreateDrawableState

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

onDraw

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

onLayout

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

verifyDrawable

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