SwitchCompat


public class SwitchCompat extends CompoundButton implements 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.

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

SwitchCompat(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)

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

Public methods

void
void
drawableHotspotChanged(float x, float y)
int
int
@Nullable 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.

@Nullable ColorStateList
@Attribute(value = "androidx.appcompat:thumbTint")
getThumbTintList()
@Nullable 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.

@Nullable ColorStateList
@Attribute(value = "androidx.appcompat:trackTint")
getTrackTintList()
@Nullable PorterDuff.Mode
@Attribute(value = "androidx.appcompat:trackTintMode")
getTrackTintMode()
boolean
void
void
void
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void
boolean
void
setAllCaps(boolean allCaps)
void
setChecked(boolean checked)
void

See setCustomSelectionActionModeCallback

void
setEmojiCompatEnabled(boolean enabled)

Configure emoji fallback behavior using EmojiCompat.

void
setFilters(@NonNull InputFilter[] filters)
void
setShowText(boolean showText)

Sets whether the on/off text should be displayed.

void
setSplitTrack(boolean splitTrack)

Specifies whether the track should be split by the thumb.

void
setSwitchMinWidth(int pixels)

Set the minimum width of the switch in pixels.

void
setSwitchPadding(int pixels)

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

void
setSwitchTextAppearance(Context context, int resid)

Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

void

Sets the typeface in which the text should be displayed on the switch.

void
setSwitchTypeface(Typeface tf, int style)

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.

void

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

void

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

void

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

void
setThumbResource(int resId)

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

void
setThumbTextPadding(int pixels)

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

void

Applies a tint to the thumb drawable.

void

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

void

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

void
setTrackResource(int resId)

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

void

Applies a tint to the track drawable.

void

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

void

Protected methods

void
final @FloatRange(from = 0.0, to = 1.0) float
int[]
onCreateDrawableState(int extraSpace)
void
onDraw(Canvas canvas)
void
onLayout(boolean changed, int left, int top, int right, int bottom)
final void
setEnforceSwitchWidth(boolean enforceSwitchWidth)

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

boolean

Inherited Constants

From android.widget.TextView
static final int
static final int
static final int
From android.view.View
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final int
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
static final String
static final String
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int
GONE = 8
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
KEEP_SCREEN_ON = 67108864
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
NO_ID = -1
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final String
VIEW_LOG_TAG = "View"
static final int
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>

Inherited methods

From android.widget.Button
PointerIcon
onResolvePointerIcon(MotionEvent event, int pointerIndex)
From android.widget.CompoundButton
From android.widget.TextView
void
addExtraDataToAccessibilityNodeInfo(
    AccessibilityNodeInfo info,
    String extraDataKey,
    Bundle arguments
)
void
final void
void
boolean
bringPointIntoView(int offset)
void
void
int
void
int
int
void
debug(int depth)
boolean
void
boolean
void
findViewsWithText(
    ArrayList<View> outViews,
    CharSequence searched,
    int flags
)
final int
int
int
int
int[]
int
String[]
int
int
int
int
BlendMode
ColorStateList
PorterDuff.Mode
Drawable[]
Drawable[]
int
int
int
int
final int
final int
ActionMode.Callback
boolean
MovementMethod
Editable
TextUtils.TruncateAt
CharSequence
int
int
InputFilter[]
int
void
int
int
String
String
boolean
int
int
Highlights
CharSequence
final ColorStateList
int
int
CharSequence
LocaleList
int
boolean
Bundle
getInputExtras(boolean create)
int
int
final KeyListener
int
final Layout
float
int
float
int
getLineBounds(int line, Rect bounds)
int
int
int
int
float
float
final ColorStateList
final boolean
int
int
int
int
int
int
int
int
int
final MovementMethod
int
getOffsetForPosition(float x, float y)
TextPaint
int
String
float
int
int
int[]
int