belongs to Maven artifact com.android.support:appcompat-v7:28.0.0-alpha1
SwitchCompat
public
class
SwitchCompat
extends CompoundButton
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.widget.TextView | ||||
↳ | android.widget.Button | ||||
↳ | android.widget.CompoundButton | ||||
↳ | android.support.v7.widget.SwitchCompat |
SwitchCompat is a version of the Switch widget which on devices back to API v7. It does not make any attempt to use the platform provided widget on those devices which it is available normally.
A Switch is a two-state toggle switch widget that can select between two
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.
See the Toggle Buttons guide.
Summary
XML attributes | |
---|---|
SwitchCompat_android_textOff |
|
SwitchCompat_android_textOn |
|
SwitchCompat_android_thumb |
|
SwitchCompat_showText |
|
SwitchCompat_splitTrack |
|
SwitchCompat_switchMinWidth |
|
SwitchCompat_switchPadding |
|
SwitchCompat_switchTextAppearance |
|
SwitchCompat_thumbTextPadding |
|
SwitchCompat_thumbTint |
|
SwitchCompat_thumbTintMode |
|
SwitchCompat_track |
|
SwitchCompat_trackTint |
|
SwitchCompat_trackTintMode |
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
|
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
| |
From
class
java.lang.Object
| |
From
interface
android.widget.Checkable
| |
From
interface
android.view.ViewTreeObserver.OnPreDrawListener
| |
From
interface
android.graphics.drawable.Drawable.Callback
| |
From
interface
android.view.KeyEvent.Callback
| |
From
interface
android.view.accessibility.AccessibilityEventSource
|
XML attributes
SwitchCompat_android_textOff
Related methods:
SwitchCompat_android_textOn
Related methods:
SwitchCompat_android_thumb
Related methods:
SwitchCompat_showText
Related methods:
SwitchCompat_splitTrack
Related methods:
SwitchCompat_switchMinWidth
Related methods:
SwitchCompat_switchPadding
Related methods:
SwitchCompat_switchTextAppearance
Related methods:
SwitchCompat_thumbTextPadding
Related methods:
SwitchCompat_thumbTint
Related methods:
SwitchCompat_thumbTintMode
Related methods:
SwitchCompat_track
Related methods:
SwitchCompat_trackTint
Related methods:
SwitchCompat_trackTintMode
Related methods:
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, AttributeSet attrs)
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, AttributeSet attrs, int defStyleAttr)
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 methods
drawableHotspotChanged
void drawableHotspotChanged (float x, float y)
Parameters | |
---|---|
x |
float |
y |
float |
getCompoundPaddingLeft
int getCompoundPaddingLeft ()
Returns | |
---|---|
int |
getCompoundPaddingRight
int getCompoundPaddingRight ()
Returns | |
---|---|
int |
getShowText
boolean getShowText ()
Related XML Attributes:
Returns | |
---|---|
boolean |
whether the on/off text should be displayed |
getSplitTrack
boolean getSplitTrack ()
Returns whether the track should be split by the thumb.
Related XML Attributes:
Returns | |
---|---|
boolean |
getSwitchMinWidth
int getSwitchMinWidth ()
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.
Related XML Attributes:
Returns | |
---|---|
int |
Minimum width of the switch in pixels |
getSwitchPadding
int getSwitchPadding ()
Get the amount of horizontal padding between the switch and the associated text.
Related XML Attributes:
Returns | |
---|---|
int |
Amount of padding in pixels |
getTextOff
CharSequence getTextOff ()
Returns the text displayed when the button is not in the checked state.
Related XML Attributes:
Returns | |
---|---|
CharSequence |
getTextOn
CharSequence getTextOn ()
Returns the text displayed when the button is in the checked state.
Related XML Attributes:
Returns | |
---|---|
CharSequence |
getThumbDrawable
Drawable getThumbDrawable ()
Get the drawable used for the switch "thumb" - the piece that the user can physically touch and drag along the track.
Related XML Attributes:
Returns | |
---|---|
Drawable |
Thumb drawable |
getThumbTextPadding
int getThumbTextPadding ()
Get the horizontal padding around the text drawn on the switch itself.
Related XML Attributes:
Returns | |
---|---|
int |
Horizontal padding for switch thumb text in pixels |
getThumbTintList
ColorStateList getThumbTintList ()
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
the tint applied to the thumb drawable |
See also:
getThumbTintMode
PorterDuff.Mode getThumbTintMode ()
Related XML Attributes:
Returns | |
---|---|
PorterDuff.Mode |
the blending mode used to apply the tint to the thumb drawable |
See also:
getTrackDrawable
Drawable getTrackDrawable ()
Get the drawable used for the track that the switch slides within.
Related XML Attributes:
Returns | |
---|---|
Drawable |
Track drawable |
getTrackTintList
ColorStateList getTrackTintList ()
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
the tint applied to the track drawable |
See also:
getTrackTintMode
PorterDuff.Mode getTrackTintMode ()
Related XML Attributes:
Returns | |
---|---|
PorterDuff.Mode |
the blending mode used to apply the tint to the track drawable |
See also:
jumpDrawablesToCurrentState
void jumpDrawablesToCurrentState ()
onInitializeAccessibilityEvent
void onInitializeAccessibilityEvent (AccessibilityEvent event)
Parameters | |
---|---|
event |
AccessibilityEvent |
onInitializeAccessibilityNodeInfo
void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)
Parameters | |
---|---|
info |
AccessibilityNodeInfo |
onMeasure
void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
Parameters | |
---|---|
widthMeasureSpec |
int |
heightMeasureSpec |
int |
onPopulateAccessibilityEvent
void onPopulateAccessibilityEvent (AccessibilityEvent event)
Parameters | |
---|---|
event |
AccessibilityEvent |
setChecked
void setChecked (boolean checked)
Parameters | |
---|---|
checked |
boolean |
setShowText
void setShowText (boolean showText)
Sets whether the on/off text should be displayed.
Related XML Attributes:
Parameters | |
---|---|
showText |
boolean : true to display on/off text |
setSplitTrack
void setSplitTrack (boolean splitTrack)
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.
Related XML Attributes:
Parameters | |
---|---|
splitTrack |
boolean : Whether the track should be split by the thumb |
setSwitchMinWidth
void setSwitchMinWidth (int pixels)
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.
Related XML Attributes:
Parameters | |
---|---|
pixels |
int : Minimum width of the switch in pixels |
setSwitchPadding
void setSwitchPadding (int pixels)
Set the amount of horizontal padding between the switch and the associated text.
Related XML Attributes:
Parameters | |
---|---|
pixels |
int : Amount of padding in pixels |
setSwitchTextAppearance
void setSwitchTextAppearance (Context context, int resid)
Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
Related XML Attributes:
Parameters | |
---|---|
context |
Context |
resid |
int |
setSwitchTypeface
void setSwitchTypeface (Typeface typeface)
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(Typeface, int)
to get the appearance
that you actually want.
Parameters | |
---|---|
typeface |
Typeface |
setSwitchTypeface
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.
Parameters | |
---|---|
tf |
Typeface |
style |
int |
setTextOff
void setTextOff (CharSequence textOff)
Sets the text displayed when the button is not in the checked state.
Related XML Attributes:
Parameters | |
---|---|
textOff |
CharSequence |
setTextOn
void setTextOn (CharSequence textOn)
Sets the text displayed when the button is in the checked state.
Related XML Attributes:
Parameters | |
---|---|
textOn |
CharSequence |
setThumbDrawable
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.
Related XML Attributes:
Parameters | |
---|---|
thumb |
Drawable : Thumb drawable |
setThumbResource
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.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : Resource ID of a thumb drawable |
setThumbTextPadding
void setThumbTextPadding (int pixels)
Set the horizontal padding around the text drawn on the switch itself.
Related XML Attributes:
Parameters | |
---|---|
pixels |
int : Horizontal padding for switch thumb text in pixels |
setThumbTintList
void setThumbTintList (ColorStateList tint)
Applies a tint to the thumb drawable. Does not modify the current
tint mode, which is SRC_IN
by default.
Subsequent calls to setThumbDrawable(Drawable)
will
automatically mutate the drawable and apply the specified tint and tint
mode using setTintList(Drawable, ColorStateList)
.
Related XML Attributes:
Parameters | |
---|---|
tint |
ColorStateList : the tint to apply, may be null to clear tint |
setThumbTintMode
void setThumbTintMode (PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by
setThumbTintList(ColorStateList)
} to the thumb drawable.
The default mode is SRC_IN
.
Related XML Attributes:
Parameters | |
---|---|
tintMode |
PorterDuff.Mode : the blending mode used to apply the tint, may be
null to clear tint |
setTrackDrawable
void setTrackDrawable (Drawable track)
Set the drawable used for the track that the switch slides within.
Related XML Attributes:
Parameters | |
---|---|
track |
Drawable : Track drawable |
setTrackResource
void setTrackResource (int resId)
Set the drawable used for the track that the switch slides within.
Related XML Attributes:
Parameters | |
---|---|
resId |
int : Resource ID of a track drawable |
setTrackTintList
void setTrackTintList (ColorStateList tint)
Applies a tint to the track drawable. Does not modify the current
tint mode, which is SRC_IN
by default.
Subsequent calls to setTrackDrawable(Drawable)
will
automatically mutate the drawable and apply the specified tint and tint
mode using setTintList(Drawable, ColorStateList)
.
Related XML Attributes:
Parameters | |
---|---|
tint |
ColorStateList : the tint to apply, may be null to clear tint |
See also:
setTrackTintMode
void setTrackTintMode (PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by
setTrackTintList(ColorStateList)
} to the track drawable.
The default mode is SRC_IN
.
Related XML Attributes:
Parameters | |
---|---|
tintMode |
PorterDuff.Mode : the blending mode used to apply the tint, may be
null to clear tint |
See also:
toggle
void toggle ()
Protected methods
drawableStateChanged
void drawableStateChanged ()
onCreateDrawableState
int[] onCreateDrawableState (int extraSpace)
Parameters | |
---|---|
extraSpace |
int |
Returns | |
---|---|
int[] |
onLayout
void onLayout (boolean changed, int left, int top, int right, int bottom)
Parameters | |
---|---|
changed |
boolean |
left |
int |
top |
int |
right |
int |
bottom |
int |
Annotations
Interfaces
- ActionMenuView.OnMenuItemClickListener
- PopupMenu.OnDismissListener
- PopupMenu.OnMenuItemClickListener
- RecyclerView.ChildDrawingOrderCallback
- RecyclerView.ItemAnimator.ItemAnimatorFinishedListener
- RecyclerView.LayoutManager.LayoutPrefetchRegistry
- RecyclerView.OnChildAttachStateChangeListener
- RecyclerView.OnItemTouchListener
- RecyclerView.RecyclerListener
- RecyclerView.SmoothScroller.ScrollVectorProvider
- SearchView.OnCloseListener
- SearchView.OnQueryTextListener
- SearchView.OnSuggestionListener
- ShareActionProvider.OnShareTargetSelectedListener
- ThemedSpinnerAdapter
- Toolbar.OnMenuItemClickListener
Classes
- ActionMenuView
- ActionMenuView.LayoutParams
- AppCompatAutoCompleteTextView
- AppCompatButton
- AppCompatCheckBox
- AppCompatCheckedTextView
- AppCompatEditText
- AppCompatImageButton
- AppCompatImageView
- AppCompatMultiAutoCompleteTextView
- AppCompatRadioButton
- AppCompatRatingBar
- AppCompatSeekBar
- AppCompatSpinner
- AppCompatTextView
- CardView
- DefaultItemAnimator
- DividerItemDecoration
- GridLayout
- GridLayout.Alignment
- GridLayout.LayoutParams
- GridLayout.Spec
- GridLayoutManager
- GridLayoutManager.DefaultSpanSizeLookup
- GridLayoutManager.LayoutParams
- GridLayoutManager.SpanSizeLookup
- LinearLayoutCompat
- LinearLayoutCompat.LayoutParams
- LinearLayoutManager
- LinearLayoutManager.LayoutChunkResult
- LinearSmoothScroller
- LinearSnapHelper
- ListPopupWindow
- OrientationHelper
- PagerSnapHelper
- PopupMenu
- RecyclerView
- RecyclerView.Adapter
- RecyclerView.AdapterDataObserver
- RecyclerView.EdgeEffectFactory
- RecyclerView.ItemAnimator
- RecyclerView.ItemAnimator.ItemHolderInfo
- RecyclerView.ItemDecoration
- RecyclerView.LayoutManager
- RecyclerView.LayoutManager.Properties
- RecyclerView.LayoutParams
- RecyclerView.OnFlingListener
- RecyclerView.OnScrollListener
- RecyclerView.RecycledViewPool
- RecyclerView.Recycler
- RecyclerView.SimpleOnItemTouchListener
- RecyclerView.SmoothScroller
- RecyclerView.SmoothScroller.Action
- RecyclerView.State
- RecyclerView.ViewCacheExtension
- RecyclerView.ViewHolder
- RecyclerViewAccessibilityDelegate
- RecyclerViewAccessibilityDelegate.ItemDelegate
- SearchView
- ShareActionProvider
- SimpleItemAnimator
- SnapHelper
- StaggeredGridLayoutManager
- StaggeredGridLayoutManager.LayoutParams
- SwitchCompat
- ThemedSpinnerAdapter.Helper
- Toolbar
- Toolbar.LayoutParams
- Toolbar.SavedState
- TooltipCompat
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-04-11 UTC.