SwitchCompat
public
class
SwitchCompat
extends CompoundButton
java.lang.Object | |||||
↳ | 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 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.
R.attr.textOn
R.attr.textOff
R.attr.switchMinWidth
R.attr.switchPadding
R.attr.switchTextAppearance
R.attr.thumb
R.attr.thumbTextPadding
R.attr.track
R.attr.thumbTint
R.attr.thumbTintMode
R.attr.trackTint
R.attr.trackTintMode
Summary
Inherited constants | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.widget.TextView
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
android.view.View
|
Inherited fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
Public constructors | |
---|---|
SwitchCompat(Context context)
Construct a new Switch with default styling. |
|
SwitchCompat(Context context, AttributeSet attrs)
Construct a new Switch with default styling, overriding specific style attributes as requested. |
|
SwitchCompat(Context context, 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
|
draw(Canvas c)
|
void
|
drawableHotspotChanged(float x, float y)
|
int
|
getCompoundPaddingLeft()
|
int
|
getCompoundPaddingRight()
|
boolean
|
getShowText()
|
boolean
|
getSplitTrack()
Returns whether the track should be split by the thumb. |
int
|
getSwitchMinWidth()
Get the minimum width of the switch in pixels. |
int
|
getSwitchPadding()
Get the amount of horizontal padding between the switch and the associated text. |
CharSequence
|
getTextOff()
Returns the text displayed when the button is not in the checked state. |
CharSequence
|
getTextOn()
Returns the text displayed when the button is in the checked state. |
Drawable
|
getThumbDrawable()
Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track. |
int
|
getThumbTextPadding()
Get the horizontal padding around the text drawn on the switch itself. |
ColorStateList
|
getThumbTintList()
|
PorterDuff.Mode
|
getThumbTintMode()
|
Drawable
|
getTrackDrawable()
Get the drawable used for the track that the switch slides within. |
ColorStateList
|
getTrackTintList()
|
PorterDuff.Mode
|
getTrackTintMode()
|
void
|
jumpDrawablesToCurrentState()
|
void
|
onInitializeAccessibilityEvent(AccessibilityEvent event)
|
void
|
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
|
void
|
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
void
|
onPopulateAccessibilityEvent(AccessibilityEvent event)
|
boolean
|
onTouchEvent(MotionEvent ev)
|
void
|
setChecked(boolean checked)
|
void
|
setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback)
See
|
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
|
setSwitchTypeface(Typeface typeface)
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
|
setTextOff(CharSequence textOff)
Sets the text displayed when the button is not in the checked state. |
void
|
setTextOn(CharSequence textOn)
Sets the text displayed when the button is in the checked state. |
void
|
setThumbDrawable(Drawable thumb)
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
|
setThumbTintList(ColorStateList tint)
Applies a tint to the thumb drawable. |
void
|
setThumbTintMode(PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by
|
void
|
setTrackDrawable(Drawable track)
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
|
setTrackTintList(ColorStateList tint)
Applies a tint to the track drawable. |
void
|
setTrackTintMode(PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by
|
void
|
toggle()
|
Protected methods | |
---|---|
void
|
drawableStateChanged()
|
int[]
|
onCreateDrawableState(int extraSpace)
|
void
|
onDraw(Canvas canvas)
|
void
|
onLayout(boolean changed, int left, int top, int right, int bottom)
|
boolean
|
verifyDrawable(Drawable who)
|
Inherited methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.widget.CompoundButton
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
android.widget.Button
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
android.widget.TextView
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
android.view.View
|