Most visited

Recently visited

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

android.support.v7.appcompat:showText Whether to draw on/off text. 
android.support.v7.appcompat:splitTrack Whether to split the track and leave a gap for the thumb drawable. 
android.support.v7.appcompat:switchMinWidth Minimum width for the switch component

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". 

android.support.v7.appcompat:switchPadding Minimum space between the switch and caption text

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". 

android.support.v7.appcompat:switchTextAppearance TextAppearance style for text displayed on the switch thumb. 
android.support.v7.appcompat:thumbTextPadding Amount of padding on either side of text within the switch thumb. 
android.support.v7.appcompat:thumbTint Tint to apply to the thumb drawable. 
android.support.v7.appcompat:thumbTintMode Blending mode used to apply the thumb tint. 
android.support.v7.appcompat:track Drawable to use as the "track" that the switch thumb slides within. 
android.support.v7.appcompat:trackTint Tint to apply to the track. 
android.support.v7.appcompat:trackTintMode Blending mode used to apply the track tint. 
android:textOff Text to use when the switch is in the unchecked/"off" state. 
android:textOn Text to use when the switch is in the checked/"on" state. 
android:thumb Drawable to use as the "thumb" that switches back and forth. 

Inherited XML attributes

From class android.widget.CompoundButton
From class android.widget.TextView
From class android.view.View

Inherited constants

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)

Manually render this view (and all of its children) to the given Canvas.

void drawableHotspotChanged(float x, float y)

This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.

int getCompoundPaddingLeft()

Returns the left padding of the view, plus space for the left Drawable if any.

int getCompoundPaddingRight()

Returns the right padding of the view, plus space for the right Drawable if any.

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()

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

void onInitializeAccessibilityEvent(AccessibilityEvent event)

Initializes an AccessibilityEvent with information about this View which is the event source.

void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

Initializes an AccessibilityNodeInfo with information about this view.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

void onPopulateAccessibilityEvent(AccessibilityEvent event)

Called from dispatchPopulateAccessibilityEvent(AccessibilityEvent) giving a chance to this View to populate the accessibility event with its text content.

boolean onTouchEvent(MotionEvent ev)

Implement this method to handle touch screen motion events.

void setChecked(boolean checked)

Changes the checked state of this button.

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 tf)

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 setThumbTintList(ColorStateList)} to the thumb drawable.

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 setTrackTintList(ColorStateList)} to the track drawable.

void toggle()

Change the checked state of the view to the inverse of its current state

Protected methods

void drawableStateChanged()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

int[] onCreateDrawableState(int extraSpace)

Generate the new Drawable state for this view.

void onDraw(Canvas canvas)

Implement this to do your drawing.

void onLayout(boolean changed, int left, int top, int right, int bottom)

Called from layout when this view should assign a size and position to each of its children.

boolean verifyDrawable(Drawable who)

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.

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

android.support.v7.appcompat:showText

Whether to draw on/off text.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.v7.appcompat:splitTrack

Whether to split the track and leave a gap for the thumb drawable.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.v7.appcompat:switchMinWidth

Minimum width for the switch component

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android.support.v7.appcompat:switchPadding

Minimum space between the switch and caption text

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android.support.v7.appcompat:switchTextAppearance

TextAppearance style for text displayed on the switch thumb.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Related methods:

android.support.v7.appcompat:thumbTextPadding

Amount of padding on either side of text within the switch thumb.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android.support.v7.appcompat:thumbTint

Tint to apply to the thumb drawable.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb#aarrggbb".

Related methods:

android.support.v7.appcompat:thumbTintMode

Blending mode used to apply the thumb tint.

Must be one of the following constant values.

ConstantValueDescription
add10Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D)
multiplyeMultiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc]
screenf[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
src_atop9The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]
src_in5The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out. [Sa * Da, Sc * Da]
src_over3The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]

Related methods:

android.support.v7.appcompat:track

Drawable to use as the "track" that the switch thumb slides within.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Related methods:

android.support.v7.appcompat:trackTint

Tint to apply to the track.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb#aarrggbb".

Related methods:

android.support.v7.appcompat:trackTintMode

Blending mode used to apply the track tint.

Must be one of the following constant values.

ConstantValueDescription
add10Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D)
multiplyeMultiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc]
screenf[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
src_atop9The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]
src_in5The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out. [Sa * Da, Sc * Da]
src_over3The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]

Related methods:

android:textOff

Text to use when the switch is in the unchecked/"off" state.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

Related methods:

android:textOn

Text to use when the switch is in the checked/"on" state.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

Related methods:

android:thumb

Drawable to use as the "thumb" that switches back and forth.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

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

draw

void draw (Canvas c)

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Parameters
c Canvas: The Canvas to which the View is rendered.

drawableHotspotChanged

void drawableHotspotChanged (float x, 
                float y)

This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.

Dispatching to child views is handled by dispatchDrawableHotspotChanged(float, float).

Be sure to call through to the superclass when overriding this function.

Parameters
x float: hotspot x coordinate
y float: hotspot y coordinate

getCompoundPaddingLeft

int getCompoundPaddingLeft ()

Returns the left padding of the view, plus space for the left Drawable if any.

Returns
int

getCompoundPaddingRight

int getCompoundPaddingRight ()

Returns the right padding of the view, plus space for the right Drawable if any.

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 ()

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

Also calls jumpToCurrentState() if there is a StateListAnimator attached to this view.

onInitializeAccessibilityEvent

void onInitializeAccessibilityEvent (AccessibilityEvent event)

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event AccessibilityEvent: The event to initialize.

onInitializeAccessibilityNodeInfo

void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info AccessibilityNodeInfo: The instance to initialize.

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

onPopulateAccessibilityEvent

void onPopulateAccessibilityEvent (AccessibilityEvent event)

Called from dispatchPopulateAccessibilityEvent(AccessibilityEvent) giving a chance to this View to populate the accessibility event with its text content. While this method is free to modify event attributes other than text content, doing so should normally be performed in onInitializeAccessibilityEvent(AccessibilityEvent).

Example: Adding formatted date string to an accessibility event in addition to the text added by the super implementation:

 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
     super.onPopulateAccessibilityEvent(event);
     final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
     String selectedDateUtterance = DateUtils.formatDateTime(mContext,
         mCurrentDate.getTimeInMillis(), flags);
     event.getText().add(selectedDateUtterance);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onPopulateAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event AccessibilityEvent: The accessibility event which to populate.

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
ev MotionEvent: The motion event.
Returns
boolean True if the event was handled, false otherwise.

setChecked

void setChecked (boolean checked)

Changes the checked state of this button.

Parameters
checked boolean: true to check the button, false to uncheck it

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 tf)

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
tf 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

See also:

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

See also:

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 ()

Change the checked state of the view to the inverse of its current state

Protected methods

drawableStateChanged

void drawableStateChanged ()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

If the View has a StateListAnimator, it will also be called to run necessary state change animations.

Be sure to call through to the superclass when overriding this function.

onCreateDrawableState

int[] onCreateDrawableState (int extraSpace)

Generate the new Drawable state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use getDrawableState().

Parameters
extraSpace int: if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states.
Returns
int[] Returns an array holding the current Drawable state of the view.

onDraw

void onDraw (Canvas canvas)

Implement this to do your drawing.

Parameters
canvas Canvas: the canvas on which the background will be drawn

onLayout

void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed boolean: This is a new size or position for this view
left int: Left position, relative to parent
top int: Top position, relative to parent
right int: Right position, relative to parent
bottom int: Bottom position, relative to parent

verifyDrawable

boolean verifyDrawable (Drawable who)

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.

Be sure to call through to the super class when overriding this function.

Parameters
who Drawable: The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.
Returns
boolean boolean If true than the Drawable is being displayed in the view; else false and it is not allowed to animate.

Hooray!