SwitchCompat

class SwitchCompat : CompoundButton, EmojiCompatConfigurationView


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:

For more information, see the Toggle Buttons guide.

Summary

Public constructors

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
Int
Int
ActionMode.Callback?
Boolean
@Attribute(value = "androidx.appcompat:showText")
getShowText()

Indicates whether the on/off text should be displayed.

Boolean
@Attribute(value = "androidx.appcompat:splitTrack")
getSplitTrack()

Returns whether the track should be split by the thumb.

Int
@Attribute(value = "androidx.appcompat:switchMinWidth")
getSwitchMinWidth()

Get the minimum width of the switch in pixels.

Int
@Attribute(value = "androidx.appcompat:switchPadding")
getSwitchPadding()

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

CharSequence!
@Attribute(value = "android:textOff")
getTextOff()

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

CharSequence!
@Attribute(value = "android:textOn")
getTextOn()

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

Drawable!
@Attribute(value = "android:thumb")
getThumbDrawable()

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")
getThumbTextPadding()

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

ColorStateList?
@Attribute(value = "androidx.appcompat:thumbTint")
getThumbTintList()
PorterDuff.Mode?
@Attribute(value = "androidx.appcompat:thumbTintMode")
getThumbTintMode()
Drawable!
@Attribute(value = "androidx.appcompat:track")
getTrackDrawable()

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

ColorStateList?
@Attribute(value = "androidx.appcompat:trackTint")
getTrackTintList()
PorterDuff.Mode?
@Attribute(value = "androidx.appcompat:trackTintMode")
getTrackTintMode()
Boolean
Unit
Unit
Unit
Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
Unit
Boolean
Unit
setAllCaps(allCaps: Boolean)
Unit
setChecked(checked: Boolean)
Unit

See setCustomSelectionActionModeCallback

Unit

Configure emoji fallback behavior using EmojiCompat.

Unit
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

Set the minimum width of the switch in pixels.

Unit

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

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

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit

Applies a tint to the thumb drawable.

Unit

Specifies the blending mode used to apply the tint specified by setThumbTintList} to the thumb drawable.

Unit

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

Unit

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

Unit

Applies a tint to the track drawable.

Unit

Specifies the blending mode used to apply the tint specified by setTrackTintList to the track drawable.

Unit

Protected functions

Unit
@FloatRange(from = 0.0, to = 1.0) Float
IntArray<Int>!
Unit
onDraw(canvas: Canvas!)
Unit
onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)
Unit
setEnforceSwitchWidth(enforceSwitchWidth: Boolean)

Sets true to enforce the switch width being at least twice of the thumb width.

Boolean

Inherited Constants

From android.widget.TextView
From android.view.View
const Int
const Int
const Int
const Int
const Int
const Int
const Property<View!, Float!>!
const Int
const String!
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
const String!
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
const String!
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
const String!
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
const String!
const String!
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
const String!
const String!
const String!
const String!
const String!
const String!
const String!
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const Int
const Int
const Int
const Int
const Int
const Int
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const Int
const Int
const Int
const Int
const Int
const Int
const Int
GONE = 8
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
KEEP_SCREEN_ON = 67108864
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
NO_ID = -1
const Int
const Int
const Int
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const IntArray<Int>!
const Property<View!, Float!>!
const Property<View!, Float!>!
const Property<View!, Float!>!
const Property<View!, Float!>!
const Property<View!, Float!>!
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const IntArray<Int>!
const IntArray<Int>!
const Int
const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int

This property is deprecated.

const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Int
const Property<View!, Float!>!
const Property<View!, Float!>!
const Property<View!, Float!>!
const String!
VIEW_LOG_TAG = "View"
const Int
const IntArray<Int>!
const Property<View!, Float!>!
const Property<View!, Float!>!
const Property<View!, Float!>!

Inherited functions

From android.widget.Button
PointerIcon!
onResolvePointerIcon(event: MotionEvent!, pointerIndex: Int)
From android.widget.CompoundButton
From android.widget.TextView
Unit
addExtraDataToAccessibilityNodeInfo(
    info: AccessibilityNodeInfo!,
    extraDataKey: String!,
    arguments: Bundle!
)
Unit
Unit
Unit
Boolean
Unit
Unit
Int
Unit
Int
Int
Unit
debug(depth: Int)
Boolean
Unit
Boolean
Unit
findViewsWithText(
    outViews: ArrayList<View!>!,
    searched: CharSequence!,
    flags: Int
)
Int
Int
Int
Int
IntArray<Int>!
Int
Array<String!>!
Int
Int
Int
Int
BlendMode!
ColorStateList!
PorterDuff.Mode!
Array<Drawable!>!
Array<Drawable!>!
Int
Int
Int
Int
Int
Int
ActionMode.Callback!
Boolean
MovementMethod!
Editable!
TextUtils.TruncateAt!
CharSequence!
Int
Int
Array<InputFilter!>!
Int
Unit
Int
Int
String!
String!
Boolean
Int
Int
Highlights!
CharSequence!
ColorStateList!
Int
Int
CharSequence!
LocaleList!
Int
Boolean
Bundle!
Int
Int
KeyListener!
Int
Layout!
Float
Int
Float
Int
getLineBounds(line: Int, bounds: Rect!)
Int
Int
Int
Int
Float
Float
ColorStateList!
Boolean
Int
Int
Int
Int
Int
Int
Int
Int
Int
MovementMethod!
Int
TextPaint!
Int
String!
Float
Int
Int
IntArray<Int>!
Int
Int
Int
Float
Float
Float
Boolean
CharSequence!
TextClassifier!
ColorStateList!
Drawable!
TextDirectionHeuristic!
Locale!
LocaleList!
PrecomputedText.Params!
Float
Drawable!
Drawable!
Drawable!
Float
Int
Int
Int
Int
Int
Int
Int
Int
TransformationMethod!
Typeface!
Array<URLSpan!>!
Boolean
Boolean
Unit
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Int
Boolean
Unit
Unit
Boolean
Unit
Unit
Unit
Unit
InputConnection!
Unit
onCreateViewTranslationRequest(
    supportedFormats: IntArray!,
    requestsCollector: Consumer<ViewTranslationRequest!>!
)
Boolean
Unit
onEditorAction(actionCode: Int)
Unit
Unit
onFocusChanged(
    focused: Boolean,
    direction: Int,
    previouslyFocusedRect: Rect!
)
Boolean
Boolean
onKeyDown(keyCode: Int, event: KeyEvent!)
Boolean
onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent!)
Boolean
onKeyPreIme(keyCode: Int, event: KeyEvent!)
Boolean
onKeyShortcut(keyCode: Int, event: KeyEvent!)
Boolean
onKeyUp(keyCode: Int, event: KeyEvent!)
Boolean
Boolean
onPrivateIMECommand(action: String!, data: Bundle!)
ContentInfo!
Unit
onRtlPropertiesChanged(layoutDirection: Int)
Unit
onScreenStateChanged(screenState: Int)
Unit
onScrollChanged(horiz: Int, vert: Int, oldHoriz: Int, oldVert: Int)
Unit
onSelectionChanged(selStart: Int, selEnd: Int)
Unit
onTextChanged(
    text: CharSequence!,
    start: Int,
    lengthBefore: Int,
    lengthAfter: Int
)
Boolean
Boolean
Unit
Unit
onVisibilityChanged(changedView: View!, visibility: Int)
Unit
onWindowFocusChanged(hasWindowFocus: Boolean)
Boolean
Unit
Unit
Unit
Unit
setAutoSizeTextTypeUniformWithConfiguration(
    autoSizeMinTextSize: Int,
    autoSizeMaxTextSize: Int,
    autoSizeStepGranularity: Int,
    unit: Int
)
Unit
setAutoSizeTextTypeUniformWithPresetSizes(
    presetSizes: IntArray!,
    unit: Int
)
Unit
Unit
setBreakStrategy(breakStrategy: Int)
Unit
Unit
Unit
Unit
Unit
setCompoundDrawables(
    left: Drawable!,
    top: Drawable!,
    right: Drawable!,
    bottom: Drawable!
)
Unit
setCompoundDrawablesRelative(
    start: Drawable!,
    top: Drawable!,
    end: Drawable!,
    bottom: Drawable!
)
Unit
setCompoundDrawablesRelativeWithIntrinsicBounds(
    start: Int,
    top: Int,
    end: Int,
    bottom: Int
)
Unit
setCompoundDrawablesWithIntrinsicBounds(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
)
Unit
Unit
Unit
Unit
Unit
Unit
setEms(ems: Int)
Unit
setEnabled(enabled: Boolean)
Unit
Unit
Unit
Unit
setFirstBaselineToTopHeight(firstBaselineToTopHeight: Int)
Unit
Unit
Unit
setFontFeatureSettings(fontFeatureSettings: String!)
Boolean
setFontVariationSettings(fontVariationSettings: String!)
Boolean
setFrame(l: Int, t: Int, r: Int, b: Int)
Unit
setFreezesText(freezesText: Boolean)
Unit
setGravity(gravity: Int)
Unit
setHeight(pixels: Int)
Unit
Unit
setHighlights(highlights: Highlights!)
Unit
Unit
Unit
Unit
setHyphenationFrequency(hyphenationFrequency: Int)
Unit
setImeActionLabel(label: CharSequence!, actionId: Int)
Unit
Unit
setImeOptions(imeOptions: Int)
Unit
Unit
setInputExtras(xmlResId: Int)
Unit
Unit
setJustificationMode(justificationMode: Int)
Unit
Unit
setLastBaselineToBottomHeight(lastBaselineToBottomHeight: Int)
Unit
setLetterSpacing(letterSpacing: Float)
Unit
setLineBreakStyle(lineBreakStyle: Int)
Unit
setLineBreakWordStyle(lineBreakWordStyle: Int)
Unit
setLineHeight(lineHeight: Int)
Unit
setLineSpacing(add: Float, mult: Float)
Unit
setLines(lines: Int)
Unit
Unit
Unit
setMarqueeRepeatLimit(marqueeLimit: Int)
Unit
setMaxEms(maxEms: Int)
Unit
setMaxHeight(maxPixels: Int)
Unit
setMaxLines(maxLines: Int)
Unit
setMaxWidth(maxPixels: Int)
Unit
setMinEms(minEms: Int)
Unit
setMinHeight(minPixels: Int)
Unit
setMinLines(minLines: Int)
Unit
setMinWidth(minPixels: Int)
Unit
Unit
Unit
setPadding(left: Int, top: Int, right: Int, bottom: Int)
Unit
setPaddingRelative(start: Int, top: Int, end: Int, bottom: Int)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
setSelectAllOnFocus(selectAllOnFocus: Boolean)
Unit
setSelected(selected: Boolean)
Unit
setShadowLayer(radius: Float, dx: Float, dy: Float, color: Int)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
setTextColor(color: Int)
Unit
setTextCursorDrawable(textCursorDrawable: Drawable!)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
setTextSelectHandle(textSelectHandle: Drawable!)
Unit
setTextSelectHandleLeft(textSelectHandleLeft: Drawable!)
Unit
setTextSelectHandleRight(textSelectHandleRight: Drawable!)
Unit
Unit
Unit
setTypeface(tf: Typeface!, style: Int)
Unit
setWidth(pixels: Int)
Boolean
From android.view.View
Unit
Unit
addFocusables(views: ArrayList<View!>!, direction: Int)
Unit
addKeyboardNavigationClusters(
    views: (Mutable)Collection<View!>!,
    direction: Int
)
Unit
Unit
Unit
Unit
ViewPropertyAnimator!
Unit
Boolean
Unit
Unit

This function is deprecated.

Unit
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Unit
Unit
Boolean
Unit
Unit
Unit
java-static Int
combineMeasuredStates(curState: Int, newState: Int)
Int
Int
WindowInsets!
computeSystemWindowInsets(in: WindowInsets!, outLocalInsets: Rect!)
Int
AccessibilityNodeInfo!
Unit
Unit

This function is deprecated.

WindowInsets!
Boolean
Unit
Unit
dispatchCreateViewTranslationRequest(
    viewIds: (Mutable)Map<AutofillId!, LongArray!>!,
    supportedFormats: IntArray!,
    capability: TranslationCapability!,
    requests: (Mutable)List<ViewTranslationRequest!>!
)
Unit
Boolean
Unit
dispatchDraw(canvas: Canvas!)
Unit
Unit
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean)
Boolean
dispatchNestedPreFling(velocityX: Float, velocityY: Float)
Boolean
dispatchNestedPrePerformAccessibilityAction(
    action: Int,
    arguments: Bundle!
)
Boolean
dispatchNestedPreScroll(
    dx: Int,
    dy: Int,
    consumed: IntArray!,
    offsetInWindow: IntArray!
)
Boolean
dispatchNestedScroll(
    dxConsumed: Int,
    dyConsumed: Int,
    dxUnconsumed: Int,
    dyUnconsumed: Int,
    offsetInWindow: IntArray!
)
Unit
Boolean
Unit
Unit
Unit
Unit
Unit
dispatchScrollCaptureSearch(
    localVisibleRect: Rect!,
    windowOffset: Point!,
    targets: Consumer<ScrollCaptureTarget!>!
)
Unit
Unit
Unit
Unit
Unit

This function is deprecated.

Boolean
Boolean
Boolean
dispatchUnhandledMove(focused: View!, direction: Int)
Unit
dispatchVisibilityChanged(changedView: View!, visibility: Int)
Unit
Unit
Unit
WindowInsets!
dispatchWindowInsetsAnimationProgress(
    insets: WindowInsets!,
    runningAnimations: (Mutable)List<WindowInsetsAnimation!>!
)
WindowInsetsAnimation.Bounds!
Unit

This function is deprecated.

Unit
View!
OnBackInvokedDispatcher!
T!
<T : View?> findViewById(id: Int)
T!
<T : View?> findViewWithTag(tag: Any!)
Boolean

This function is deprecated.

View!
focusSearch(direction: Int)
Unit
forceHasOverlappingRendering(hasOverlappingRendering: Boolean)
Unit
Boolean
Unit
generateDisplayHash(
    hashAlgorithm: String!,
    bounds: Rect!,
    executor: Executor!,
    callback: DisplayHashResultCallback!
)
java-static Int
View.AccessibilityDelegate!
Int
AccessibilityNodeProvider!
CharSequence!
Int
Int
String!
String!
Float
Animation!
Matrix!
IBinder!
IntArray<Int>!
(Mutable)Map<Int!, Int!>!
AutofillId!
Drawable!
BlendMode!
ColorStateList!
PorterDuff.Mode!
Int
Float
Float
Rect!
Boolean
getClipBounds(outRect: Rect!)
Boolean
ContentCaptureSession!
CharSequence!
Context!
ContextMenu.ContextMenuInfo!
Boolean
java-static Int
getDefaultSize(size: Int, measureSpec: Int)
Display!
IntArray<Int>!
Bitmap!

This function is deprecated.

Int

This function is deprecated.

Int

This function is deprecated.

Unit
getDrawingRect(outRect: Rect!)
Long
Float
Int
Boolean
Boolean
Int
ArrayList<View!>!
getFocusables(direction: Int)
Drawable!
Int
BlendMode!
ColorStateList!
PorterDuff.Mode!
Boolean
getGlobalVisibleRect(r: Rect!, globalOffset: Point!)
Handler!
Float
Float
Float
Float
Runnable!
Boolean
Int
Unit
getHitRect(outRect: Rect!)
Int
Int
Drawable!
Drawable!
Int
Int
Int
Int
Boolean
KeyEvent.DispatcherState!
Int
Int
Int
ViewGroup.LayoutParams!
Int
Boolean
Unit
Unit
Unit
Matrix!
Int
Int
Int
Int
Int
Int
Int
Int
Int
Int
Int
Int
Int
View.OnFocusChangeListener!
Int
ViewOutlineProvider!
Int
Int
ViewOverlay!
Int
Int
Int
Int
Int
Int
ViewParent!
ViewParent!
Float
Float
PointerIcon!
(Mutable)List<Rect!>!
Array<String!>!
Resources!
Boolean
Int
AttachedSurfaceControl!
View!
WindowInsets!
Float
Float
Float
Float
Float
Int
Int
Int
Int
Int
Int
Int
Int
Int
Int
CharSequence!
StateListAnimator!
Int
Int
(Mutable)List<Rect!>!
Int

This function is deprecated.

Any!
Int
Int
CharSequence!
Int
Float
TouchDelegate!
ArrayList<View!>!
Float
String!
Float
Float
Float
Long
Int
Int
Drawable!
Drawable!
Int
ViewTranslationResponse!
ViewTreeObserver!
Int
Int
Int
WindowId!
WindowInsetsController!
Int

This function is deprecated.

IBinder!
Int
Unit
Float
Float
Float
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
java-static View!
inflate(context: Context!, resource: Int, root: ViewGroup!)
Unit
invalidate(dirty: Rect!)

This function is deprecated.

Unit
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean

This function is deprecated.

Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
View!
keyboardNavigationClusterSearch(currentCluster: View!, direction: Int)
Unit
layout(l: Int, t: Int, r: Int, b: Int)
Unit
measure(widthMeasureSpec: Int, heightMeasureSpec: Int)
java-static IntArray<Int>!
mergeDrawableStates(baseState: IntArray!, additionalState: IntArray!)
Unit
Unit
Unit
Unit
WindowInsets!
Unit
Boolean
Unit
onCreateVirtualViewTranslationRequests(
    virtualIds: LongArray!,
    supportedFormats: IntArray!,
    requestsCollector: Consumer<ViewTranslationRequest!>!
)
Unit
Unit
Unit
Unit
Boolean
Unit
Unit
Unit
Boolean
Boolean
onKeyLongPress(keyCode: Int, event: KeyEvent!)
Unit
onOverScrolled(
    scrollX: Int,
    scrollY: Int,
    clampedX: Boolean,
    clampedY: Boolean
)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
onScrollCaptureSearch(
    localVisibleRect: Rect!,
    windowOffset: Point!,
    targets: Consumer<ScrollCaptureTarget!>!
)
Boolean
onSetAlpha(alpha: Int)
Unit
onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)
Unit
Unit
Unit
Unit

This function is deprecated.

Unit
Boolean
overScrollBy(
    deltaX: Int,
    deltaY: Int,
    scrollX: Int,
    scrollY: Int,
    scrollRangeX: Int,
    scrollRangeY: Int,
    maxOverScrollX: Int,
    maxOverScrollY: Int,
    isTouchEvent: Boolean
)
Boolean
performAccessibilityAction(action: Int, arguments: Bundle!)
Boolean
Boolean
performHapticFeedback(feedbackConstant: Int)
ContentInfo!
Unit
playSoundEffect(soundConstant: Int)
Boolean
post(action: Runnable!)
Boolean
postDelayed(action: Runnable!, delayMillis: Long)
Unit
Unit
postInvalidateDelayed(delayMilliseconds: Long)
Unit
Unit
Unit
postOnAnimationDelayed(action: Runnable!, delayMillis: Long)
Unit
Unit
Boolean
Unit
Unit
Unit
Unit
Unit

This function is deprecated.

Boolean
Boolean
Unit
Unit
Boolean
Unit
T!
<T : View?> requireViewById(id: Int)
Unit
java-static Int
resolveSize(size: Int, measureSpec: Int)
java-static Int
resolveSizeAndState(size: Int, measureSpec: Int, childMeasuredState: Int)
Boolean
Unit
Unit
saveAttributeDataForStyleable(
    context: Context!,
    styleable: IntArray!,
    attrs: AttributeSet!,
    t: TypedArray!,
    defStyleAttr: Int,
    defStyleRes: Int
)
Unit
Unit
scheduleDrawable(who: Drawable!, what: Runnable!, when: Long)
Unit
scrollBy(x: Int, y: Int)
Unit
scrollTo(x: Int, y: Int)
Unit
Unit
setAccessibilityDataSensitive(accessibilityDataSensitive: Int)
Unit
Unit
Unit
Unit
setAccessibilityPaneTitle(accessibilityPaneTitle: CharSequence!)
Unit
Unit
Unit
setActivated(activated: Boolean)
Unit
setAllowClickWhenDisabled(clickableWhenDisabled: Boolean)
Unit
Unit
Unit
setAlpha(alpha: Float)
Unit
setAnimation(animation: Animation!)
Unit
Unit
Unit
setAutofillHints(autofillHints: Array<String!>!)
Unit
Unit
setBackground(background: Drawable!)
Unit
Unit

This function is deprecated.

Unit
Unit
Unit
Unit
Unit
setBottom(bottom: Int)
Unit
Unit
setClickable(clickable: Boolean)
Unit
setClipBounds(clipBounds: Rect!)
Unit
setClipToOutline(clipToOutline: Boolean)
Unit
Unit
setContentDescription(contentDescription: CharSequence!)
Unit
setContextClickable(contextClickable: Boolean)
Unit
setDefaultFocusHighlightEnabled(defaultFocusHighlightEnabled: Boolean)
Unit

This function is deprecated.

Unit

This function is deprecated.

Unit

This function is deprecated.

Unit
Unit
setElevation(elevation: Float)
Unit
Unit
Unit
setFitsSystemWindows(fitSystemWindows: Boolean)
Unit
setFocusable(focusable: Boolean)
Unit
setFocusableInTouchMode(focusableInTouchMode: Boolean)
Unit
setFocusedByDefault(isFocusedByDefault: Boolean)
Unit
Unit
setForeground(foreground: Drawable!)
Unit
Unit
Unit
Unit
Unit
setHandwritingBoundsOffsets(
    offsetLeft: Float,
    offsetTop: Float,
    offsetRight: Float,
    offsetBottom: Float
)
Unit
Unit
setHapticFeedbackEnabled(hapticFeedbackEnabled: Boolean)
Unit
setHasTransientState(hasTransientState: Boolean)
Unit
setHorizontalFadingEdgeEnabled(horizontalFadingEdgeEnabled: Boolean)
Unit
setHorizontalScrollBarEnabled(horizontalScrollBarEnabled: Boolean)
Unit
Unit
Unit
setHovered(hovered: Boolean)
Unit
setId(id: Int)
Unit
Unit
Unit
Unit
setIsCredential(isCredential: Boolean)
Unit
setIsHandwritingDelegate(isHandwritingDelegate: Boolean)
Unit
setKeepScreenOn(keepScreenOn: Boolean)
Unit
Unit
Unit
Unit
setLayerType(layerType: Int, paint: Paint!)
Unit
setLayoutDirection(layoutDirection: Int)
Unit
Unit
setLeft(left: Int)
Unit
setLeftTopRightBottom(left: Int, top: Int, right: Int, bottom: Int)
Unit
setLongClickable(longClickable: Boolean)
Unit
setMeasuredDimension(measuredWidth: Int, measuredHeight: Int)
Unit
setMinimumHeight(minHeight: Int)
Unit
setMinimumWidth(minWidth: Int)
Unit
Unit
setNextClusterForwardId(nextClusterForwardId: Int)
Unit
setNextFocusDownId(nextFocusDownId: Int)
Unit
setNextFocusForwardId(nextFocusForwardId: Int)
Unit
setNextFocusLeftId(nextFocusLeftId: Int)
Unit
setNextFocusRightId(nextFocusRightId: Int)
Unit
setNextFocusUpId(nextFocusUpId: Int)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
setOnReceiveContentListener(
    mimeTypes: Array<String!>!,
    listener: OnReceiveContentListener!
)
Unit
Unit

This function is deprecated.

Unit
Unit
Unit
Unit
Unit
setOverScrollMode(overScrollMode: Int)
Unit
setPivotX(pivotX: Float)
Unit
setPivotY(pivotY: Float)
Unit
setPointerIcon(pointerIcon: PointerIcon!)
Unit
setPreferKeepClear(preferKeepClear: Boolean)
Unit
Unit
setPressed(pressed: Boolean)
Unit
setRenderEffect(renderEffect: RenderEffect!)
Unit
setRevealOnFocusHint(revealOnFocus: Boolean)
Unit
setRight(right: Int)
Unit
setRotation(rotation: Float)
Unit
setRotationX(rotationX: Float)
Unit
setRotationY(rotationY: Float)
Unit
Unit
Unit
setScaleX(scaleX: Float)
Unit
setScaleY(scaleY: Float)
Unit
setScreenReaderFocusable(screenReaderFocusable: Boolean)
Unit
setScrollBarDefaultDelayBeforeFade(
    scrollBarDefaultDelayBeforeFade: Int
)
Unit
setScrollBarFadeDuration(scrollBarFadeDuration: Int)
Unit
setScrollBarSize(scrollBarSize: Int)
Unit
Unit
Unit
Unit
setScrollContainer(isScrollContainer: Boolean)
Unit
setScrollIndicators(indicators: Int)
Unit
setScrollX(value: Int)
Unit
setScrollY(value: Int)
Unit
Unit
setSoundEffectsEnabled(soundEffectsEnabled: Boolean)
Unit
Unit
Unit

This function is deprecated.

Unit
setTag(tag: Any!)
Unit
setTextAlignment(textAlignment: Int)
Unit
setTextDirection(textDirection: Int)
Unit
Unit
setTop(top: Int)
Unit
Unit
Unit
setTransitionName(transitionName: String!)
Unit
Unit
setTranslationX(translationX: Float)
Unit
setTranslationY(translationY: Float)
Unit
setTranslationZ(translationZ: Float)
Unit
setVerticalFadingEdgeEnabled(verticalFadingEdgeEnabled: Boolean)
Unit
setVerticalScrollBarEnabled(verticalScrollBarEnabled: Boolean)
Unit
Unit
Unit
Unit
Unit
setVisibility(visibility: Int)
Unit
setWillNotCacheDrawing(willNotCacheDrawing: Boolean)

This function is deprecated.

Unit
setWillNotDraw(willNotDraw: Boolean)
Unit
Unit
setX(x: Float)
Unit
setY(y: Float)
Unit
setZ(z: Float)
ActionMode!
Unit
Boolean
startDrag(
    data: ClipData!,
    shadowBuilder: View.DragShadowBuilder!,
    myLocalState: Any!,
    flags: Int
)

This function is deprecated.

Boolean
startDragAndDrop(
    data: ClipData!,
    shadowBuilder: View.DragShadowBuilder!,
    myLocalState: Any!,
    flags: Int
)
Boolean
Unit
String!
Unit
Unit
Unit
Unit
Boolean

This function is deprecated.

Boolean

Public constructors

SwitchCompat

Added in 1.1.0
SwitchCompat(context: Context)

Construct a new Switch with default styling.

Parameters
context: Context

The Context that will determine this widget's theming.

SwitchCompat

Added in 1.1.0
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

Added in 1.1.0
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

draw

fun draw(c: Canvas): Unit

drawableHotspotChanged

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

getCompoundPaddingLeft

fun getCompoundPaddingLeft(): Int

getCompoundPaddingRight

fun getCompoundPaddingRight(): Int

getCustomSelectionActionModeCallback

fun getCustomSelectionActionModeCallback(): ActionMode.Callback?

getShowText

Added in 1.1.0
@Attribute(value = "androidx.appcompat:showText")
fun getShowText(): Boolean

Indicates whether the on/off text should be displayed.

Returns
Boolean

true if the on/off text should be displayed, otherwise false

See also
showText

getSplitTrack

Added in 1.1.0
@Attribute(value = "androidx.appcompat:splitTrack")
fun getSplitTrack(): Boolean

Returns whether the track should be split by the thumb.

See also
splitTrack

getSwitchMinWidth

Added in 1.1.0
@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

Added in 1.1.0
@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

Added in 1.1.0
@Attribute(value = "android:textOff")
fun getTextOff(): CharSequence!

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

See also
textOff

getTextOn

Added in 1.1.0
@Attribute(value = "android:textOn")
fun getTextOn(): CharSequence!

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

See also
textOn

getThumbDrawable

Added in 1.1.0
@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

Added in 1.1.0
@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

Added in 1.1.0
@Attribute(value = "androidx.appcompat:thumbTint")
fun getThumbTintList(): ColorStateList?
Returns
ColorStateList?

the tint applied to the thumb drawable

getThumbTintMode

Added in 1.1.0
@Attribute(value = "androidx.appcompat:thumbTintMode")
fun getThumbTintMode(): PorterDuff.Mode?
Returns
PorterDuff.Mode?

the blending mode used to apply the tint to the thumb drawable

getTrackDrawable

Added in 1.1.0
@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

Added in 1.1.0
@Attribute(value = "androidx.appcompat:trackTint")
fun getTrackTintList(): ColorStateList?
Returns
ColorStateList?

the tint applied to the track drawable

getTrackTintMode

Added in 1.1.0
@Attribute(value = "androidx.appcompat:trackTintMode")
fun getTrackTintMode(): PorterDuff.Mode?
Returns
PorterDuff.Mode?

the blending mode used to apply the tint to the track drawable

isEmojiCompatEnabled

Added in 1.7.0-alpha03
fun isEmojiCompatEnabled(): Boolean
Returns
Boolean

the current enabled state, set via setEmojiCompatEnabled

jumpDrawablesToCurrentState

fun jumpDrawablesToCurrentState(): Unit

onInitializeAccessibilityEvent

fun onInitializeAccessibilityEvent(event: AccessibilityEvent!): Unit

onInitializeAccessibilityNodeInfo

fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo!): Unit

onMeasure

Added in 1.1.0
fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

onPopulateAccessibilityEvent

fun onPopulateAccessibilityEvent(event: AccessibilityEvent!): Unit

onTouchEvent

fun onTouchEvent(ev: MotionEvent!): Boolean

setAllCaps

fun setAllCaps(allCaps: Boolean): Unit

setChecked

fun setChecked(checked: Boolean): Unit

setCustomSelectionActionModeCallback

fun setCustomSelectionActionModeCallback(
    actionModeCallback: ActionMode.Callback?
): Unit

See setCustomSelectionActionModeCallback

setEmojiCompatEnabled

Added in 1.7.0-alpha03
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)

setFilters

fun setFilters(filters: Array<InputFilter!>): Unit

setShowText

Added in 1.1.0
fun setShowText(showText: Boolean): Unit

Sets whether the on/off text should be displayed.

Parameters
showText: Boolean

true to display on/off text

See also
showText

setSplitTrack

Added in 1.1.0
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

Added in 1.1.0
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

Added in 1.1.0
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

Added in 1.1.0
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 1.1.0
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

Added in 1.1.0
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

Added in 1.1.0
fun setTextOff(textOff: CharSequence!): Unit

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

See also
textOff

setTextOn

Added in 1.1.0
fun setTextOn(textOn: CharSequence!): Unit

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

See also
textOn

setThumbDrawable

Added in 1.1.0
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

Added in 1.1.0
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

Added in 1.1.0
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

Added in 1.1.0
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 null to clear tint

setThumbTintMode

Added in 1.1.0
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 null to clear tint

setTrackDrawable

Added in 1.1.0
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

Added in 1.1.0
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

Added in 1.1.0
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 null to clear tint

setTrackTintMode

Added in 1.1.0
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 null to clear tint

toggle

fun toggle(): Unit

Protected functions

drawableStateChanged

protected fun drawableStateChanged(): Unit

getThumbPosition

Added in 1.5.0
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).

onCreateDrawableState

protected fun onCreateDrawableState(extraSpace: Int): IntArray<Int>!

onDraw

protected fun onDraw(canvas: Canvas!): Unit

onLayout

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

setEnforceSwitchWidth

Added in 1.5.0
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.

verifyDrawable

protected fun verifyDrawable(who: Drawable): Boolean