MaterialSwitch

public class MaterialSwitch
extends SwitchCompat

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
         ↳ android.widget.CompoundButton
           ↳ androidx.appcompat.widget.SwitchCompat
             ↳ com.google.android.material.materialswitch.MaterialSwitch


A class that creates a Material Themed Switch. This class is intended to provide a brand new Switch design and replace the obsolete SwitchMaterial class.

For more information, see the component developer guidance and design guidelines.

Summary

XML attributes

MaterialSwitch_thumbIcon  
MaterialSwitch_thumbIconSize  
MaterialSwitch_thumbIconTint  
MaterialSwitch_thumbIconTintMode  
MaterialSwitch_trackDecoration  
MaterialSwitch_trackDecorationTint  
MaterialSwitch_trackDecorationTintMode  

Inherited constants

Inherited fields

Public constructors

MaterialSwitch(Context context)
MaterialSwitch(Context context, AttributeSet attrs)
MaterialSwitch(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

Drawable getThumbDrawable()
Drawable getThumbIconDrawable()

Gets the drawable used for the thumb icon that will be drawn upon the thumb.

int getThumbIconSize()

Returns the size of the thumb icon.

ColorStateList getThumbIconTintList()

Returns the tint applied to the thumb icon drawable

PorterDuff.Mode getThumbIconTintMode()

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

ColorStateList getThumbTintList()
Drawable getTrackDecorationDrawable()

Get the drawable used for the track decoration that will be drawn upon the track.

ColorStateList getTrackDecorationTintList()

Returns the tint applied to the track decoration drawable

PorterDuff.Mode getTrackDecorationTintMode()

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

Drawable getTrackDrawable()
ColorStateList getTrackTintList()
void invalidate()
void setThumbDrawable(Drawable drawable)
void setThumbIconDrawable(Drawable icon)

Sets the drawable used for the thumb icon that will be drawn upon the thumb.

void setThumbIconResource(int resId)

Sets the drawable used for the thumb icon that will be drawn upon the thumb.

void setThumbIconSize(int size)

Sets the size of the thumb icon.

void setThumbIconTintList(ColorStateList tintList)

Applies a tint to the thumb icon drawable.

void setThumbIconTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setThumbIconTintList(ColorStateList)} to the thumb icon drawable.

void setThumbTintList(ColorStateList tintList)
void setThumbTintMode(PorterDuff.Mode tintMode)
void setTrackDecorationDrawable(Drawable trackDecoration)

Set the drawable used for the track decoration that will be drawn upon the track.

void setTrackDecorationResource(int resId)

Set the drawable used for the track decoration that will be drawn upon the track.

void setTrackDecorationTintList(ColorStateList tintList)

Applies a tint to the track decoration drawable.

void setTrackDecorationTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setTrackDecorationTintList(ColorStateList)} to the track decoration drawable.

void setTrackDrawable(Drawable track)
void setTrackTintList(ColorStateList tintList)
void setTrackTintMode(PorterDuff.Mode tintMode)

Protected methods

int[] onCreateDrawableState(int extraSpace)

Inherited methods

XML attributes

MaterialSwitch_thumbIcon

Related methods:

MaterialSwitch_thumbIconSize

Related methods:

MaterialSwitch_thumbIconTint

Related methods:

MaterialSwitch_thumbIconTintMode

Related methods:

MaterialSwitch_trackDecoration

Related methods:

MaterialSwitch_trackDecorationTint

Related methods:

MaterialSwitch_trackDecorationTintMode

Related methods:

Public constructors

MaterialSwitch

public MaterialSwitch (Context context)

Parameters
context Context

MaterialSwitch

public MaterialSwitch (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

MaterialSwitch

public MaterialSwitch (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

getThumbDrawable

public Drawable getThumbDrawable ()

Returns
Drawable

getThumbIconDrawable

public Drawable getThumbIconDrawable ()

Gets the drawable used for the thumb icon that will be drawn upon the thumb.

Related XML Attributes:

Returns
Drawable

getThumbIconSize

public int getThumbIconSize ()

Returns the size of the thumb icon.

Related XML Attributes:

Returns
int

getThumbIconTintList

public ColorStateList getThumbIconTintList ()

Returns the tint applied to the thumb icon drawable

Related XML Attributes:

Returns
ColorStateList

getThumbIconTintMode

public PorterDuff.Mode getThumbIconTintMode ()

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

Related XML Attributes:

Returns
PorterDuff.Mode

getThumbTintList

public ColorStateList getThumbTintList ()

Returns
ColorStateList

getTrackDecorationDrawable

public Drawable getTrackDecorationDrawable ()

Get the drawable used for the track decoration that will be drawn upon the track.

Related XML Attributes:

Returns
Drawable

getTrackDecorationTintList

public ColorStateList getTrackDecorationTintList ()

Returns the tint applied to the track decoration drawable

Related XML Attributes:

Returns
ColorStateList

getTrackDecorationTintMode

public PorterDuff.Mode getTrackDecorationTintMode ()

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

Related XML Attributes:

Returns
PorterDuff.Mode

getTrackDrawable

public Drawable getTrackDrawable ()

Returns
Drawable

getTrackTintList

public ColorStateList getTrackTintList ()

Returns
ColorStateList

invalidate

public void invalidate ()

setThumbDrawable

public void setThumbDrawable (Drawable drawable)

Parameters
drawable Drawable

setThumbIconDrawable

public void setThumbIconDrawable (Drawable icon)

Sets the drawable used for the thumb icon that will be drawn upon the thumb.

Related XML Attributes:

Parameters
icon Drawable: Thumb icon drawable

setThumbIconResource

public void setThumbIconResource (int resId)

Sets the drawable used for the thumb icon that will be drawn upon the thumb.

Related XML Attributes:

Parameters
resId int: Resource ID of a thumb icon drawable

setThumbIconSize

public void setThumbIconSize (int size)

Sets the size of the thumb icon.

Related XML Attributes:

Parameters
size int

setThumbIconTintList

public void setThumbIconTintList (ColorStateList tintList)

Applies a tint to the thumb icon drawable. Does not modify the current tint mode, which is PorterDuff.Mode.SRC_IN by default.

Subsequent calls to setThumbIconDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using DrawableCompat.setTintList(Drawable, ColorStateList).

Related XML Attributes:

Parameters
tintList ColorStateList: the tint to apply, may be null to clear tint

setThumbIconTintMode

public void setThumbIconTintMode (PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setThumbIconTintList(ColorStateList)} to the thumb icon drawable. The default mode is PorterDuff.Mode.SRC_IN.

Related XML Attributes:

Parameters
tintMode PorterDuff.Mode: the blending mode used to apply the tint

setThumbTintList

public void setThumbTintList (ColorStateList tintList)

Parameters
tintList ColorStateList

setThumbTintMode

public void setThumbTintMode (PorterDuff.Mode tintMode)

Parameters
tintMode PorterDuff.Mode

setTrackDecorationDrawable

public void setTrackDecorationDrawable (Drawable trackDecoration)

Set the drawable used for the track decoration that will be drawn upon the track.

Related XML Attributes:

Parameters
trackDecoration Drawable: Track decoration drawable

setTrackDecorationResource

public void setTrackDecorationResource (int resId)

Set the drawable used for the track decoration that will be drawn upon the track.

Related XML Attributes:

Parameters
resId int: Resource ID of a track decoration drawable

setTrackDecorationTintList

public void setTrackDecorationTintList (ColorStateList tintList)

Applies a tint to the track decoration drawable. Does not modify the current tint mode, which is PorterDuff.Mode.SRC_IN by default.

Subsequent calls to setTrackDecorationDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using DrawableCompat.setTintList(Drawable, ColorStateList).

Related XML Attributes:

Parameters
tintList ColorStateList: the tint to apply, may be null to clear tint

setTrackDecorationTintMode

public void setTrackDecorationTintMode (PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setTrackDecorationTintList(ColorStateList)} to the track decoration drawable. The default mode is PorterDuff.Mode.SRC_IN.

Related XML Attributes:

Parameters
tintMode PorterDuff.Mode: the blending mode used to apply the tint

setTrackDrawable

public void setTrackDrawable (Drawable track)

Parameters
track Drawable

setTrackTintList

public void setTrackTintList (ColorStateList tintList)

Parameters
tintList ColorStateList

setTrackTintMode

public void setTrackTintMode (PorterDuff.Mode tintMode)

Parameters
tintMode PorterDuff.Mode

Protected methods

onCreateDrawableState

protected int[] onCreateDrawableState (int extraSpace)

Parameters
extraSpace int

Returns
int[]