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 in which the text should be displayed on the switch.

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