Added in API level 14

Switch


open class Switch : CompoundButton
kotlin.Any
   ↳ android.view.View
   ↳ android.widget.TextView
   ↳ android.widget.Button
   ↳ android.widget.CompoundButton
   ↳ android.widget.Switch

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.

The text property controls the text of the switch label. The textOn and textOff properties control the text of the thumb. The textAppearance property and the related setTypeface() methods control the typeface and style of the switch label. The switchTextAppearance property and the related setSwitchTypeface() methods control the typeface and style of the thumb text.

Note: The thumb text is displayed only if the showText attribute is set to true. See also setShowText(boolean) and getShowText().

androidx.appcompat.widget.SwitchCompat provides backward compatibility down to Android 4.0 (API level 14).

For more information, see the Toggle Buttons guide.

Summary

XML attributes
android:switchMinWidth Minimum width for the switch component.
android:switchPadding Minimum space between the switch and caption text.
android:switchTextAppearance TextAppearance style for text displayed on the switch thumb.
android:textOff Text to use when the switch is in the unchecked/"off" state.
android:textOn Text to use when the switch is in the checked/"on" state.
android:thumb Drawable to use as the "thumb" that switches back and forth.
android:thumbTextPadding Amount of padding on either side of text within the switch thumb.
android:track Drawable to use as the "track" that the switch thumb slides within.
Inherited XML attributes
Inherited constants
Public constructors
Switch(context: Context!)

Construct a new Switch with default styling.

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

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

Switch(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.

Switch(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

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

Public methods
open Unit

open Unit

open CharSequence!

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

open ColorStateList?

open PorterDuff.Mode?

open Drawable!

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

open BlendMode?

open ColorStateList?

open PorterDuff.Mode?

open Unit

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

open Boolean

open Unit
setChecked(checked: Boolean)

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 - to the specified Icon.

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

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

open Unit

Applies a tint to the thumb drawable.

open Unit

Specifies the blending mode used to apply the tint specified by setThumbTintList(android.content.res.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 to the specified Icon.

open Unit

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

open Unit

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

open Unit

Applies a tint to the track drawable.

open Unit

Specifies the blending mode used to apply the tint specified by setTrackTintList(android.content.res.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

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.

Inherited functions
Inherited properties

XML attributes

android:switchMinWidth

android:switchMinWidth
Minimum width for the switch component.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:switchPadding

android:switchPadding
Minimum space between the switch and caption text.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:switchTextAppearance

android:switchTextAppearance
TextAppearance style for text displayed on the switch thumb.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:textOff

android:textOff
Text to use when the switch is in the unchecked/"off" state.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

android:textOn

android:textOn
Text to use when the switch is in the checked/"on" state.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

android:thumb

android:thumb
Drawable to use as the "thumb" that switches back and forth.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:thumbTextPadding

android:thumbTextPadding
Amount of padding on either side of text within the switch thumb.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:track

android:track
Drawable to use as the "track" that the switch thumb slides within.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Public constructors

Switch

Added in API level 14
Switch(context: Context!)

Construct a new Switch with default styling.

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

Switch

Added in API level 14
Switch(
    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 AttributeSet!: Specification of attributes that should deviate from default styling.

Switch

Added in API level 14
Switch(
    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 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.

Switch

Added in API level 21
Switch(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

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 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.
defStyleRes Int: 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 0 to not look for defaults.

Public methods

draw

Added in API level 14
open fun draw(c: Canvas): Unit
Parameters
canvas The Canvas to which the View is rendered. This value cannot be null.

drawableHotspotChanged

Added in API level 21
open fun drawableHotspotChanged(
    x: Float,
    y: Float
): Unit
Parameters
x Float: hotspot x coordinate
y Float: hotspot y coordinate

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

getCompoundPaddingLeft

Added in API level 14
open fun getCompoundPaddingLeft(): Int

getCompoundPaddingRight

Added in API level 14
open fun getCompoundPaddingRight(): Int

getShowText

Added in API level 21
open fun getShowText(): Boolean
Return
Boolean whether the on/off text should be displayed

getSplitTrack

Added in API level 21
open fun getSplitTrack(): Boolean

Returns whether the track should be split by the thumb.

getSwitchMinWidth

Added in API level 16
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

getSwitchPadding

Added in API level 16
open fun getSwitchPadding(): Int

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

Return
Int Amount of padding in pixels

getTextOff

Added in API level 14
open fun getTextOff(): CharSequence!

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

getTextOn

Added in API level 14
open fun getTextOn(): CharSequence!

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

getThumbDrawable

Added in API level 16
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

getThumbTextPadding

Added in API level 16
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

getThumbTintBlendMode

Added in API level 29
open fun getThumbTintBlendMode(): BlendMode?
Return
BlendMode? the blending mode used to apply the tint to the thumb drawable This value may be null.

getThumbTintList

Added in API level 23
open fun getThumbTintList(): ColorStateList?
Return
ColorStateList? the tint applied to the thumb drawable This value may be null.

getThumbTintMode

Added in API level 23
open fun getThumbTintMode(): PorterDuff.Mode?
Return
PorterDuff.Mode? the blending mode used to apply the tint to the thumb drawable This value may be null.

getTrackDrawable

Added in API level 16
open fun getTrackDrawable(): Drawable!

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

Return
Drawable! Track drawable

getTrackTintBlendMode

Added in API level 29
open fun getTrackTintBlendMode(): BlendMode?
Return
BlendMode? the blending mode used to apply the tint to the track drawable This value may be null.

getTrackTintList

Added in API level 23
open fun getTrackTintList(): ColorStateList?
Return
ColorStateList? the tint applied to the track drawable This value may be null.

getTrackTintMode

Added in API level 23
open fun getTrackTintMode(): PorterDuff.Mode?
Return
PorterDuff.Mode? the blending mode used to apply the tint to the track drawable This value may be null.

jumpDrawablesToCurrentState

Added in API level 14
open fun jumpDrawablesToCurrentState(): Unit

onMeasure

Added in API level 14
open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit
Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.

onTouchEvent

Added in API level 14
open fun onTouchEvent(ev: MotionEvent!): Boolean
Parameters
event The motion event.
Return
Boolean True if the event was handled, false otherwise.

setChecked

Added in API level 14
open fun setChecked(checked: Boolean): Unit
Parameters
checked Boolean: true to check the button, false to uncheck it

setShowText

Added in API level 21
open fun setShowText(showText: Boolean): Unit

Sets whether the on/off text should be displayed.

Parameters
showText Boolean: true to display on/off text

setSplitTrack

Added in API level 21
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

setSwitchMinWidth

Added in API level 16
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

setSwitchPadding

Added in API level 16
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

setSwitchTextAppearance

Added in API level 14
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.

setSwitchTypeface

Added in API level 14
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

Added in API level 14
open fun setSwitchTypeface(tf: 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(android.graphics.Typeface,int) to get the appearance that you actually want.

setTextOff

Added in API level 14
open fun setTextOff(textOff: CharSequence!): Unit

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

setTextOn

Added in API level 14
open fun setTextOn(textOn: CharSequence!): Unit

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

setThumbDrawable

Added in API level 16
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

setThumbIcon

Added in API level 31
open fun setThumbIcon(icon: Icon?): Unit

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

Parameters
icon Icon?: an Icon holding the desired thumb, or null to clear the thumb

setThumbResource

Added in API level 16
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

setThumbTextPadding

Added in API level 16
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

setThumbTintBlendMode

Added in API level 29
open fun setThumbTintBlendMode(blendMode: BlendMode?): Unit

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

Parameters
blendMode BlendMode?: the blending mode used to apply the tint, may be null to clear tint

setThumbTintList

Added in API level 23
open fun setThumbTintList(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(android.graphics.drawable.Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using Drawable#setTintList(ColorStateList).

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

setThumbTintMode

Added in API level 23
open fun setThumbTintMode(tintMode: PorterDuff.Mode?): Unit

Specifies the blending mode used to apply the tint specified by setThumbTintList(android.content.res.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

setTrackDrawable

Added in API level 16
open fun setTrackDrawable(track: Drawable!): Unit

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

Parameters
track Drawable!: Track drawable

setTrackIcon

Added in API level 31
open fun setTrackIcon(icon: Icon?): Unit

Set the drawable used for the track that the switch slides within to the specified Icon.

Parameters
icon Icon?: an Icon holding the desired track, or null to clear the track

setTrackResource

Added in API level 16
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

setTrackTintBlendMode

Added in API level 29
open fun setTrackTintBlendMode(blendMode: BlendMode?): Unit

Specifies the blending mode used to apply the tint specified by setTrackTintList(android.content.res.ColorStateList)} to the track drawable. The default mode is BlendMode#SRC_IN.

Parameters
blendMode BlendMode?: the blending mode used to apply the tint, may be null to clear tint

setTrackTintList

Added in API level 23
open fun setTrackTintList(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(android.graphics.drawable.Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using Drawable#setTintList(ColorStateList).

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

setTrackTintMode

Added in API level 23
open fun setTrackTintMode(tintMode: PorterDuff.Mode?): Unit

Specifies the blending mode used to apply the tint specified by setTrackTintList(android.content.res.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

toggle

Added in API level 14
open fun toggle(): Unit

Protected methods

drawableStateChanged

Added in API level 14
protected open fun drawableStateChanged(): Unit

onCreateDrawableState

Added in API level 14
protected open fun onCreateDrawableState(extraSpace: Int): IntArray!
Parameters
extraSpace Int: 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.
Return
IntArray! Returns an array holding the current Drawable state of the view.

onDraw

Added in API level 14
protected open fun onDraw(canvas: Canvas): Unit
Parameters
canvas Canvas: the canvas on which the background will be drawn This value cannot be null.

onLayout

Added in API level 14
protected open fun onLayout(
    changed: Boolean,
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Unit
Parameters
changed Boolean: This is a new size or position for this view
left Int: Left position, relative to parent
top Int: Top position, relative to parent
right Int: Right position, relative to parent
bottom Int: Bottom position, relative to parent

verifyDrawable

Added in API level 14
protected open fun verifyDrawable(who: Drawable): Boolean

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.
If you override this method you must call through to the superclass implementation.

Parameters
who Drawable: This value cannot be null.
Return
Boolean boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate.