SwitchCompat
class SwitchCompat : CompoundButton, EmojiCompatConfigurationView
| 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 Switch widget that brings the visuals and functionality of the toggle widget to older versions of the Android platform. Unlike other widgets in this package, SwitchCompat is not automatically used in layouts that include the <Switch> element. Instead, you need to explicitly use <androidx.appcompat.widget.SwitchCompat> and the matching attributes in your layouts.
The thumb can be tinted with setThumbTintList and setThumbTintMode APIs as well as with the matching XML attributes. The track can be tinted with setTrackTintList and setTrackTintMode APIs as well as with the matching XML attributes.
Supported attributes include:
textOntextOffswitchMinWidthswitchPaddingswitchTextAppearancethumbthumbTextPaddingtrackthumbTintthumbTintModetrackTinttrackTintMode
For more information, see the Toggle Buttons guide.
Summary
Public constructors |
|---|
SwitchCompat(context: Context)Construct a new Switch with default styling. |
SwitchCompat(context: Context, attrs: AttributeSet?)Construct a new Switch with default styling, overriding specific style attributes as requested. |
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. |
Public functions |
|
|---|---|
Unit |
|
Unit |
drawableHotspotChanged(x: Float, y: Float) |
Int |
|
Int |
|
ActionMode.Callback? |
|
Boolean |
@Attribute(value = "androidx.appcompat:showText")Indicates whether the on/off text should be displayed. |
Boolean |
@Attribute(value = "androidx.appcompat:splitTrack")Returns whether the track should be split by the thumb. |
Int |
@Attribute(value = "androidx.appcompat:switchMinWidth")Get the minimum width of the switch in pixels. |
Int |
@Attribute(value = "androidx.appcompat:switchPadding")Get the amount of horizontal padding between the switch and the associated text. |
CharSequence! |
@Attribute(value = "android:textOff")Returns the text displayed when the button is not in the checked state. |
CharSequence! |
Returns the text displayed when the button is in the checked state. |
Drawable! |
@Attribute(value = "android:thumb")Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
Int |
@Attribute(value = "androidx.appcompat:thumbTextPadding")Get the horizontal padding around the text drawn on the switch itself. |
ColorStateList? |
@Attribute(value = "androidx.appcompat:thumbTint") |
PorterDuff.Mode? |
@Attribute(value = "androidx.appcompat:thumbTintMode") |
Drawable! |
@Attribute(value = "androidx.appcompat:track")Get the drawable used for the track that the switch slides within. |
ColorStateList? |
@Attribute(value = "androidx.appcompat:trackTint") |
PorterDuff.Mode? |
@Attribute(value = "androidx.appcompat:trackTintMode") |
Boolean |
|
Unit |
|
Unit |
|
Unit |
|
Unit |
|
Unit |
|
Boolean |
onTouchEvent(ev: MotionEvent!) |
Unit |
setAllCaps(allCaps: Boolean) |
Unit |
setChecked(checked: Boolean) |
Unit |
setCustomSelectionActionModeCallback( |
Unit |
setEmojiCompatEnabled(enabled: Boolean)Configure emoji fallback behavior using EmojiCompat. |
Unit |
setFilters(filters: Array<InputFilter!>) |
Unit |
setShowText(showText: Boolean)Sets whether the on/off text should be displayed. |
Unit |
setSplitTrack(splitTrack: Boolean)Specifies whether the track should be split by the thumb. |
Unit |
setSwitchMinWidth(pixels: Int)Set the minimum width of the switch in pixels. |
Unit |
setSwitchPadding(pixels: Int)Set the amount of horizontal padding between the switch and the associated text. |
Unit |
setSwitchTextAppearance(context: Context!, resid: Int)Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource. |
Unit |
setSwitchTypeface(typeface: Typeface!)Sets the typeface in which the text should be displayed on the switch. |
Unit |
setSwitchTypeface(tf: Typeface!, style: Int)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. |
Unit |
setTextOff(textOff: CharSequence!)Sets the text displayed when the button is not in the checked state. |
Unit |
setTextOn(textOn: CharSequence!)Sets the text displayed when the button is in the checked state. |
Unit |
setThumbDrawable(thumb: Drawable!)Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
Unit |
setThumbResource(resId: Int)Set the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
Unit |
setThumbTextPadding(pixels: Int)Set the horizontal padding around the text drawn on the switch itself. |
Unit |
setThumbTintList(tint: ColorStateList?)Applies a tint to the thumb drawable. |
Unit |
setThumbTintMode(tintMode: PorterDuff.Mode?)Specifies the blending mode used to apply the tint specified by |
Unit |
setTrackDrawable(track: Drawable!)Set the drawable used for the track that the switch slides within. |
Unit |
setTrackResource(resId: Int)Set the drawable used for the track that the switch slides within. |
Unit |
setTrackTintList(tint: ColorStateList?)Applies a tint to the track drawable. |
Unit |
setTrackTintMode(tintMode: PorterDuff.Mode?)Specifies the blending mode used to apply the tint specified by |
Unit |
toggle() |
Protected functions |
|
|---|---|
Unit |
|
@FloatRange(from = 0.0, to = 1.0) Float |
|
IntArray<Int>! |
onCreateDrawableState(extraSpace: Int) |
Unit |
|
Unit |
|
Unit |
setEnforceSwitchWidth(enforceSwitchWidth: Boolean)Sets |
Boolean |
verifyDrawable(who: Drawable) |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
SwitchCompat
SwitchCompat(context: Context)
Construct a new Switch with default styling.
| Parameters | |
|---|---|
context: Context |
The Context that will determine this widget's theming. |
SwitchCompat
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: AttributeSet? |
Specification of attributes that should deviate from default styling. |
SwitchCompat
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: 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. |
Public functions
getCustomSelectionActionModeCallback
fun getCustomSelectionActionModeCallback(): ActionMode.Callback?
getShowText
@Attribute(value = "androidx.appcompat:showText")
fun getShowText(): Boolean
Indicates whether the on/off text should be displayed.
| Returns | |
|---|---|
Boolean |
|
| See also | |
|---|---|
showText |
getSplitTrack
@Attribute(value = "androidx.appcompat:splitTrack")
fun getSplitTrack(): Boolean
Returns whether the track should be split by the thumb.
| See also | |
|---|---|
splitTrack |
getSwitchMinWidth
@Attribute(value = "androidx.appcompat:switchMinWidth")
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.
| Returns | |
|---|---|
Int |
Minimum width of the switch in pixels |
| See also | |
|---|---|
switchMinWidth |
getSwitchPadding
@Attribute(value = "androidx.appcompat:switchPadding")
fun getSwitchPadding(): Int
Get the amount of horizontal padding between the switch and the associated text.
| Returns | |
|---|---|
Int |
Amount of padding in pixels |
| See also | |
|---|---|
switchPadding |
getTextOff
@Attribute(value = "android:textOff")
fun getTextOff(): CharSequence!
Returns the text displayed when the button is not in the checked state.
| See also | |
|---|---|
textOff |
getTextOn
@Attribute(value = "android:textOn")
fun getTextOn(): CharSequence!
Returns the text displayed when the button is in the checked state.
| See also | |
|---|---|
textOn |
getThumbDrawable
@Attribute(value = "android:thumb")
fun getThumbDrawable(): Drawable!
Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
| Returns | |
|---|---|
Drawable! |
Thumb drawable |
| See also | |
|---|---|
thumb |
getThumbTextPadding
@Attribute(value = "androidx.appcompat:thumbTextPadding")
fun getThumbTextPadding(): Int
Get the horizontal padding around the text drawn on the switch itself.
| Returns | |
|---|---|
Int |
Horizontal padding for switch thumb text in pixels |
| See also | |
|---|---|
thumbTextPadding |
getThumbTintList
@Attribute(value = "androidx.appcompat:thumbTint")
fun getThumbTintList(): ColorStateList?
| Returns | |
|---|---|
ColorStateList? |
the tint applied to the thumb drawable |
| See also | |
|---|---|
setThumbTintList |
|
thumbTint |
getThumbTintMode
@Attribute(value = "androidx.appcompat:thumbTintMode")
fun getThumbTintMode(): PorterDuff.Mode?
| Returns | |
|---|---|
PorterDuff.Mode? |
the blending mode used to apply the tint to the thumb drawable |
| See also | |
|---|---|
setThumbTintMode |
|
thumbTintMode |
getTrackDrawable
@Attribute(value = "androidx.appcompat:track")
fun getTrackDrawable(): Drawable!
Get the drawable used for the track that the switch slides within.
| Returns | |
|---|---|
Drawable! |
Track drawable |
| See also | |
|---|---|
track |
getTrackTintList
@Attribute(value = "androidx.appcompat:trackTint")
fun getTrackTintList(): ColorStateList?
| Returns | |
|---|---|
ColorStateList? |
the tint applied to the track drawable |
| See also | |
|---|---|
setTrackTintList |
|
trackTint |
getTrackTintMode
@Attribute(value = "androidx.appcompat:trackTintMode")
fun getTrackTintMode(): PorterDuff.Mode?
| Returns | |
|---|---|
PorterDuff.Mode? |
the blending mode used to apply the tint to the track drawable |
| See also | |
|---|---|
setTrackTintMode |
|
trackTintMode |
isEmojiCompatEnabled
fun isEmojiCompatEnabled(): Boolean
| Returns | |
|---|---|
Boolean |
the current enabled state, set via |
onInitializeAccessibilityNodeInfo
fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo!): Unit
setCustomSelectionActionModeCallback
fun setCustomSelectionActionModeCallback(
actionModeCallback: ActionMode.Callback?
): Unit
setEmojiCompatEnabled
fun setEmojiCompatEnabled(enabled: Boolean): Unit
Configure emoji fallback behavior using EmojiCompat. When enabled, this View will attempt to use EmojiCompat to enabled missing emojis. When disabled, this View will not display missing emojis using EmojiCompat. EmojiCompat must be correctly configured on a device for this to have an effect, which will happen by default if a correct downloadable fonts provider is installed on the device. If you manually configure EmojiCompat by calling EmojiCompat init after this View is constructed, you may call this method again to enable EmojiCompat on this text view. For more information about EmojiCompat configuration see the emoji2 module.
| Parameters | |
|---|---|
enabled: Boolean |
if true, display missing emoji using EmojiCompat, otherwise display missing emoji using a fallback glyph "□" (known as tofu) |
setShowText
fun setShowText(showText: Boolean): Unit
Sets whether the on/off text should be displayed.
| Parameters | |
|---|---|
showText: Boolean |
|
| See also | |
|---|---|
showText |
setSplitTrack
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 |
| See also | |
|---|---|
splitTrack |
setSwitchMinWidth
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 |
| See also | |
|---|---|
switchMinWidth |
setSwitchPadding
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 |
| See also | |
|---|---|
switchPadding |
setSwitchTextAppearance
fun setSwitchTextAppearance(context: Context!, resid: Int): Unit
Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
| See also | |
|---|---|
switchTextAppearance |
setSwitchTypeface
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 to get the appearance that you actually want.
setSwitchTypeface
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.
setTextOff
fun setTextOff(textOff: CharSequence!): Unit
Sets the text displayed when the button is not in the checked state.
| See also | |
|---|---|
textOff |
setTextOn
fun setTextOn(textOn: CharSequence!): Unit
Sets the text displayed when the button is in the checked state.
| See also | |
|---|---|
textOn |
setThumbDrawable
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 |
| See also | |
|---|---|
thumb |
setThumbResource
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 |
| See also | |
|---|---|
thumb |
setThumbTextPadding
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 |
| See also | |
|---|---|
thumbTextPadding |
setThumbTintList
fun setThumbTintList(tint: ColorStateList?): Unit
Applies a tint to the thumb drawable. Does not modify the current tint mode, which is SRC_IN by default.
Subsequent calls to setThumbDrawable will automatically mutate the drawable and apply the specified tint and tint mode using setTintList.
| Parameters | |
|---|---|
tint: ColorStateList? |
the tint to apply, may be |
| See also | |
|---|---|
getThumbTintList |
|
thumbTint |
|
setTintList |
setThumbTintMode
fun setThumbTintMode(tintMode: PorterDuff.Mode?): Unit
Specifies the blending mode used to apply the tint specified by setThumbTintList} to the thumb drawable. The default mode is SRC_IN.
| Parameters | |
|---|---|
tintMode: PorterDuff.Mode? |
the blending mode used to apply the tint, may be |
| See also | |
|---|---|
getThumbTintMode |
|
thumbTintMode |
|
setTintMode |
setTrackDrawable
fun setTrackDrawable(track: Drawable!): Unit
Set the drawable used for the track that the switch slides within.
| Parameters | |
|---|---|
track: Drawable! |
Track drawable |
| See also | |
|---|---|
track |
setTrackResource
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 |
| See also | |
|---|---|
track |
setTrackTintList
fun setTrackTintList(tint: ColorStateList?): Unit
Applies a tint to the track drawable. Does not modify the current tint mode, which is SRC_IN by default.
Subsequent calls to setTrackDrawable will automatically mutate the drawable and apply the specified tint and tint mode using setTintList.
| Parameters | |
|---|---|
tint: ColorStateList? |
the tint to apply, may be |
| See also | |
|---|---|
getTrackTintList |
|
trackTint |
setTrackTintMode
fun setTrackTintMode(tintMode: PorterDuff.Mode?): Unit
Specifies the blending mode used to apply the tint specified by setTrackTintList to the track drawable. The default mode is SRC_IN.
| Parameters | |
|---|---|
tintMode: PorterDuff.Mode? |
the blending mode used to apply the tint, may be |
| See also | |
|---|---|
getTrackTintMode |
|
trackTintMode |
Protected functions
getThumbPosition
protected fun getThumbPosition(): @FloatRange(from = 0.0, to = 1.0) Float
| Returns | |
|---|---|
@FloatRange(from = 0.0, to = 1.0) Float |
the current thumb position as a decimal value between 0 (off) and 1 (on). |
onLayout
protected fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int): Unit
setEnforceSwitchWidth
protected fun setEnforceSwitchWidth(enforceSwitchWidth: Boolean): Unit
Sets true to enforce the switch width being at least twice of the thumb width. Otherwise the switch width will be the value set by setSwitchMinWidth. The default value is true.