SwitchCompat
public class SwitchCompat extends CompoundButton implements EmojiCompatConfigurationView
| 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 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(@NonNull Context context)Construct a new Switch with default styling. |
SwitchCompat(@NonNull Context context, @Nullable AttributeSet attrs)Construct a new Switch with default styling, overriding specific style attributes as requested. |
SwitchCompat(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")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. |
@Nullable ColorStateList |
@Attribute(value = "androidx.appcompat:thumbTint") |
@Nullable 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. |
@Nullable ColorStateList |
@Attribute(value = "androidx.appcompat:trackTint") |
@Nullable PorterDuff.Mode |
@Attribute(value = "androidx.appcompat:trackTintMode") |
boolean |
|
void |
|
void |
|
void |
|
void |
onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
void |
|
boolean |
|
void |
setAllCaps(boolean allCaps) |
void |
setChecked(boolean checked) |
void |
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 |
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 |
Applies a tint to the thumb drawable. |
void |
setThumbTintMode(@Nullable 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 |
Applies a tint to the track drawable. |
void |
setTrackTintMode(@Nullable PorterDuff.Mode tintMode)Specifies the blending mode used to apply the tint specified by |
void |
toggle() |
Protected methods |
|
|---|---|
void |
|
final @FloatRange(from = 0.0, to = 1.0) float |
|
int[] |
onCreateDrawableState(int extraSpace) |
void |
|
void |
onLayout(boolean changed, int left, int top, int right, int bottom) |
final void |
setEnforceSwitchWidth(boolean enforceSwitchWidth)Sets |
boolean |
verifyDrawable(@NonNull Drawable who) |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
SwitchCompat
public SwitchCompat(@NonNull Context context)
Construct a new Switch with default styling.
SwitchCompat
public SwitchCompat(@NonNull Context context, @Nullable AttributeSet attrs)
Construct a new Switch with default styling, overriding specific style attributes as requested.
| Parameters | |
|---|---|
@NonNull Context context |
The Context that will determine this widget's theming. |
@Nullable AttributeSet attrs |
Specification of attributes that should deviate from default styling. |
SwitchCompat
public 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.
| Parameters | |
|---|---|
@NonNull Context context |
The Context that will determine this widget's theming. |
@Nullable AttributeSet attrs |
Specification of attributes that should deviate from the default styling. |
int defStyleAttr |
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
getCustomSelectionActionModeCallback
public @Nullable ActionMode.Callback getCustomSelectionActionModeCallback()
getShowText
@Attribute(value = "androidx.appcompat:showText")
public boolean getShowText()
Indicates whether the on/off text should be displayed.
| Returns | |
|---|---|
boolean |
|
| See also | |
|---|---|
showText |
getSplitTrack
@Attribute(value = "androidx.appcompat:splitTrack")
public boolean getSplitTrack()
Returns whether the track should be split by the thumb.
| See also | |
|---|---|
splitTrack |
getSwitchMinWidth
@Attribute(value = "androidx.appcompat:switchMinWidth")
public 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.
| Returns | |
|---|---|
int |
Minimum width of the switch in pixels |
| See also | |
|---|---|
switchMinWidth |
getSwitchPadding
@Attribute(value = "androidx.appcompat:switchPadding")
public int getSwitchPadding()
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")
public CharSequence getTextOff()
Returns the text displayed when the button is not in the checked state.
| See also | |
|---|---|
textOff |
getTextOn
@Attribute(value = "android:textOn")
public CharSequence getTextOn()
Returns the text displayed when the button is in the checked state.
| See also | |
|---|---|
textOn |
getThumbDrawable
@Attribute(value = "android:thumb")
public Drawable getThumbDrawable()
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")
public int getThumbTextPadding()
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")
public @Nullable ColorStateList getThumbTintList()
| Returns | |
|---|---|
@Nullable ColorStateList |
the tint applied to the thumb drawable |
| See also | |
|---|---|
setThumbTintList |
|
thumbTint |
getThumbTintMode
@Attribute(value = "androidx.appcompat:thumbTintMode")
public @Nullable PorterDuff.Mode getThumbTintMode()
| Returns | |
|---|---|
@Nullable PorterDuff.Mode |
the blending mode used to apply the tint to the thumb drawable |
| See also | |
|---|---|
setThumbTintMode |
|
thumbTintMode |
getTrackDrawable
@Attribute(value = "androidx.appcompat:track")
public Drawable getTrackDrawable()
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")
public @Nullable ColorStateList getTrackTintList()
| Returns | |
|---|---|
@Nullable ColorStateList |
the tint applied to the track drawable |
| See also | |
|---|---|
setTrackTintList |
|
trackTint |
getTrackTintMode
@Attribute(value = "androidx.appcompat:trackTintMode")
public @Nullable PorterDuff.Mode getTrackTintMode()
| Returns | |
|---|---|
@Nullable PorterDuff.Mode |
the blending mode used to apply the tint to the track drawable |
| See also | |
|---|---|
setTrackTintMode |
|
trackTintMode |
isEmojiCompatEnabled
public boolean isEmojiCompatEnabled()
| Returns | |
|---|---|
boolean |
the current enabled state, set via |
onInitializeAccessibilityNodeInfo
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
setCustomSelectionActionModeCallback
public void setCustomSelectionActionModeCallback(
@Nullable ActionMode.Callback actionModeCallback
)
setEmojiCompatEnabled
public void setEmojiCompatEnabled(boolean enabled)
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 | |
|---|---|
boolean enabled |
if true, display missing emoji using EmojiCompat, otherwise display missing emoji using a fallback glyph "□" (known as tofu) |
setShowText
public void setShowText(boolean showText)
Sets whether the on/off text should be displayed.
| Parameters | |
|---|---|
boolean showText |
|
| See also | |
|---|---|
showText |
setSplitTrack
public 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.
| Parameters | |
|---|---|
boolean splitTrack |
Whether the track should be split by the thumb |
| See also | |
|---|---|
splitTrack |
setSwitchMinWidth
public 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.
| Parameters | |
|---|---|
int pixels |
Minimum width of the switch in pixels |
| See also | |
|---|---|
switchMinWidth |
setSwitchPadding
public void setSwitchPadding(int pixels)
Set the amount of horizontal padding between the switch and the associated text.
| Parameters | |
|---|---|
int pixels |
Amount of padding in pixels |
| See also | |
|---|---|
switchPadding |
setSwitchTextAppearance
public void setSwitchTextAppearance(Context context, int resid)
Sets the switch text color, size, style, hint color, and highlight color from the specified TextAppearance resource.
| See also | |
|---|---|
switchTextAppearance |
setSwitchTypeface
public 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 to get the appearance that you actually want.
setSwitchTypeface
public 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.
setTextOff
public void setTextOff(CharSequence textOff)
Sets the text displayed when the button is not in the checked state.
| See also | |
|---|---|
textOff |
setTextOn
public void setTextOn(CharSequence textOn)
Sets the text displayed when the button is in the checked state.
| See also | |
|---|---|
textOn |
setThumbDrawable
public 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.
| Parameters | |
|---|---|
Drawable thumb |
Thumb drawable |
| See also | |
|---|---|
thumb |
setThumbResource
public 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.
| Parameters | |
|---|---|
int resId |
Resource ID of a thumb drawable |
| See also | |
|---|---|
thumb |
setThumbTextPadding
public void setThumbTextPadding(int pixels)
Set the horizontal padding around the text drawn on the switch itself.
| Parameters | |
|---|---|
int pixels |
Horizontal padding for switch thumb text in pixels |
| See also | |
|---|---|
thumbTextPadding |
setThumbTintList
public void setThumbTintList(@Nullable 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 will automatically mutate the drawable and apply the specified tint and tint mode using setTintList.
| Parameters | |
|---|---|
@Nullable ColorStateList tint |
the tint to apply, may be |
| See also | |
|---|---|
getThumbTintList |
|
thumbTint |
|
setTintList |
setThumbTintMode
public void setThumbTintMode(@Nullable PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by setThumbTintList} to the thumb drawable. The default mode is SRC_IN.
| Parameters | |
|---|---|
@Nullable PorterDuff.Mode tintMode |
the blending mode used to apply the tint, may be |
| See also | |
|---|---|
getThumbTintMode |
|
thumbTintMode |
|
setTintMode |
setTrackDrawable
public void setTrackDrawable(Drawable track)
Set the drawable used for the track that the switch slides within.
| Parameters | |
|---|---|
Drawable track |
Track drawable |
| See also | |
|---|---|
track |
setTrackResource
public void setTrackResource(int resId)
Set the drawable used for the track that the switch slides within.
| Parameters | |
|---|---|
int resId |
Resource ID of a track drawable |
| See also | |
|---|---|
track |
setTrackTintList
public void setTrackTintList(@Nullable 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 will automatically mutate the drawable and apply the specified tint and tint mode using setTintList.
| Parameters | |
|---|---|
@Nullable ColorStateList tint |
the tint to apply, may be |
| See also | |
|---|---|
getTrackTintList |
|
trackTint |
setTrackTintMode
public void setTrackTintMode(@Nullable PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by setTrackTintList to the track drawable. The default mode is SRC_IN.
| Parameters | |
|---|---|
@Nullable PorterDuff.Mode tintMode |
the blending mode used to apply the tint, may be |
| See also | |
|---|---|
getTrackTintMode |
|
trackTintMode |
Protected methods
getThumbPosition
protected final @FloatRange(from = 0.0, to = 1.0) float getThumbPosition()
| Returns | |
|---|---|
@FloatRange(from = 0.0, to = 1.0) float |
the current thumb position as a decimal value between 0 (off) and 1 (on). |
setEnforceSwitchWidth
protected final void setEnforceSwitchWidth(boolean enforceSwitchWidth)
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.