Google is committed to advancing racial equity for Black communities. See how.

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 complete backport of the core android.widget.Switch widget that brings the visuals and the functionality of that widget to older versions of the platform. Unlike other widgets in this package, SwitchCompat is not automatically used in layouts that use the <Switch> element. Instead, you need to explicitly use <androidx.appcompat.widget.SwitchCompat> and the matching attributes in your layouts.

A Switch is a two-state toggle switch widget that can be used to select one of the two available 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.

The thumb can be tinted with setThumbTintList(ColorStateList) and setThumbTintMode(PorterDuff.Mode) APIs, as well as with the matching XML attributes. The track can be tinted with setTrackTintList(ColorStateList) and setTrackTintMode(PorterDuff.Mode) APIs, as well as with the matching XML attributes.

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

Summary

Public constructors
<init>(@NonNull context: Context)

Construct a new Switch with default styling.

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

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

<init>(@NonNull context: Context, @Nullable 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