Google is committed to advancing racial equity for Black communities. See how.

Slider

public class Slider
extends View

java.lang.Object
   ↳ android.view.View
     ↳ com.google.android.material.slider.Slider


A widget that allows picking a value within a given range by sliding a thumb along a horizontal line.

android:value: Optional. The initial value of the slider. If not specified, the slider's minimum value android:valueFrom is used.

Summary

Nested classes

interface Slider.OnChangeListener

Interface definition for a callback invoked when a slider's value is changed. 

interface Slider.OnSliderTouchListener

Interface definition for callbacks invoked when a slider's touch event is being started/stopped. 

XML attributes

Slider_android_stepSize  
Slider_android_value  
Slider_android_valueFrom  
Slider_android_valueTo  
Slider_haloColor  
Slider_haloRadius  
Slider_labelBehavior  
Slider_thumbColor  
Slider_thumbElevation  
Slider_thumbRadius  
Slider_tickColor  
Slider_tickColorActive  
Slider_tickColorInactive  
Slider_trackColor  
Slider_trackColorActive  
Slider_trackColorInactive  
Slider_trackHeight  

Inherited constants

From class android.view.View

Inherited fields

From class android.view.View

Public constructors

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

Public methods

void addOnChangeListener(L listener)

Registers a callback to be invoked when the slider changes.

void addOnSliderTouchListener(T listener)

Registers a callback to be invoked when the slider touch event is being started or stopped

void clearOnChangeListeners()

Removes all instances of attached to this slider

void clearOnSliderTouchListeners()

Removes all instances of touch listeners attached to this slider

boolean dispatchHoverEvent(MotionEvent event)
boolean dispatchKeyEvent(KeyEvent event)
CharSequence getAccessibilityClassName()
int getActiveThumbIndex()

Returns the index of the currently active thumb, or -1 if no thumb is active

int getFocusedThumbIndex()

Returns the index of the currently focused thumb

int getHaloRadius()

Returns the radius of the halo.

ColorStateList getHaloTintList()

Returns the color of the halo.

int getLabelBehavior()

Returns the ERROR(/LabelBehavior) used.

float getStepSize()

Returns the step size used to mark the ticks.

float getThumbElevation()

Returns the elevation of the thumb.

int getThumbRadius()

Returns the radius of the thumb.

ColorStateList getThumbTintList()

Returns the color of the thumb.

ColorStateList getTickActiveTintList()

Returns the color of the ticks on the active portion of the track.

ColorStateList getTickInactiveTintList()

Returns the color of the ticks on the inactive portion of the track.

ColorStateList getTickTintList()

Returns the color of the tick if the active and inactive parts aren't different.

ColorStateList getTrackActiveTintList()

Returns the color of the active portion of the track.

int getTrackHeight()

Returns the height of the track in pixels.

ColorStateList getTrackInactiveTintList()

Returns the color of the inactive portion of the track.

int getTrackSidePadding()

Returns the side padding of the track.

ColorStateList getTrackTintList()

Returns the color of the track if the active and inactive parts aren't different.

int getTrackWidth()

Returns the width of the track in pixels.

float getValue()

Returns the value of the slider.

float getValueFrom()

Returns the slider's valueFrom value.

float getValueTo()

Returns the slider's valueTo value.

boolean hasLabelFormatter()

Returns true if the slider has a LabelFormatter attached, false otherwise.

boolean onKeyDown(int keyCode, KeyEvent event)
boolean onKeyUp(int keyCode, KeyEvent event)
boolean onTouchEvent(MotionEvent event)
void removeOnChangeListener(L listener)

Removes a callback for value changes from this slider.

void removeOnSliderTouchListener(T listener)

Removes a callback to be invoked when the slider touch event is being started or stopped

void setEnabled(boolean enabled)
void setFocusedThumbIndex(int index)

Sets the index of the currently focused thumb

void setHaloRadius(int radius)

Sets the radius of the halo in pixels.

void setHaloRadiusResource(int radius)

Sets the radius of the halo from a dimension resource.

void setHaloTintList(ColorStateList haloColor)

Sets the color of the halo.

void setLabelBehavior(int labelBehavior)

Determines the ERROR(/LabelBehavior) used.

void setLabelFormatter(LabelFormatter formatter)

Registers a LabelFormatter to be used to format the value displayed in the bubble shown when the slider operates in discrete mode.

void setStepSize(float stepSize)

Sets the step size to use to mark the ticks.

void setThumbElevation(float elevation)

Sets the elevation of the thumb.

void setThumbElevationResource(int elevation)

Sets the elevation of the thumb from a dimension resource.

void setThumbRadius(int radius)

Sets the radius of the thumb in pixels.

void setThumbRadiusResource(int radius)

Sets the radius of the thumb from a dimension resource.

void setThumbTintList(ColorStateList thumbColor)

Sets the color of the thumb.

void setTickActiveTintList(ColorStateList tickColor)

Sets the color of the ticks on the active portion of the track.

void setTickInactiveTintList(ColorStateList tickColor)

Sets the color of the ticks on the inactive portion of the track.

void setTickTintList(ColorStateList tickColor)

Sets the color of the tick marks.

void setTrackActiveTintList(ColorStateList trackColor)

Sets the color of the active portion of the track.

void setTrackHeight(int trackHeight)

Set the height of the track in pixels.

void setTrackInactiveTintList(ColorStateList trackColor)

Sets the color of the inactive portion of the track.

void setTrackTintList(ColorStateList trackColor)

Sets the color of the track.

void setValue(float value)

Sets the value of the slider.

void setValueFrom(float valueFrom)

Sets the slider's valueFrom value.

void setValueTo(float valueTo)

Sets the slider's valueTo value.

Protected methods

void drawableStateChanged()
void onAttachedToWindow()
void onDetachedFromWindow()
void onDraw(Canvas canvas)
void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()
void onSizeChanged(int w, int h, int oldw, int oldh)
boolean pickActiveThumb()

Tries to pick the active thumb if one hasn't already been set.

void setActiveThumbIndex(int index)

Inherited methods

From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML attributes

Slider_android_stepSize

Related methods:

Slider_android_value

Related methods:

Slider_android_valueFrom

Related methods:

Slider_android_valueTo

Related methods:

Slider_haloColor

Related methods:

Slider_haloRadius

Related methods:

Slider_labelBehavior

Related methods:

Slider_thumbColor

Related methods:

Slider_thumbElevation

Related methods:

Slider_thumbRadius

Related methods:

Slider_tickColor

Related methods:

Slider_tickColorActive

Related methods:

Slider_tickColorInactive

Related methods:

Slider_trackColor

Related methods:

Slider_trackColorActive

Related methods:

Slider_trackColorInactive

Related methods:

Slider_trackHeight

Related methods:

Public constructors

Slider

Slider (Context context)

Parameters
context Context

Slider

Slider (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Slider

Slider (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addOnChangeListener

void addOnChangeListener (L listener)

Registers a callback to be invoked when the slider changes.

Parameters
listener L: The callback to run when the slider changes

addOnSliderTouchListener

void addOnSliderTouchListener (T listener)

Registers a callback to be invoked when the slider touch event is being started or stopped

Parameters
listener T: The callback to run when the slider starts or stops being touched

clearOnChangeListeners

void clearOnChangeListeners ()

Removes all instances of attached to this slider

clearOnSliderTouchListeners

void clearOnSliderTouchListeners ()

Removes all instances of touch listeners attached to this slider

dispatchHoverEvent

boolean dispatchHoverEvent (MotionEvent event)

Parameters
event MotionEvent

Returns
boolean

dispatchKeyEvent

boolean dispatchKeyEvent (KeyEvent event)

Parameters
event KeyEvent

Returns
boolean

getAccessibilityClassName

CharSequence getAccessibilityClassName ()

Returns
CharSequence

getActiveThumbIndex

int getActiveThumbIndex ()

Returns the index of the currently active thumb, or -1 if no thumb is active

Returns
int

getFocusedThumbIndex

int getFocusedThumbIndex ()

Returns the index of the currently focused thumb

Returns
int

getHaloRadius

int getHaloRadius ()

Returns the radius of the halo.

Related XML Attributes:

Returns
int

getHaloTintList

ColorStateList getHaloTintList ()

Returns the color of the halo.

Related XML Attributes:

Returns
ColorStateList

getLabelBehavior

int getLabelBehavior ()

Returns the ERROR(/LabelBehavior) used.

Related XML Attributes:

Returns
int

getStepSize

float getStepSize ()

Returns the step size used to mark the ticks.

A step size of 0 means that the slider is operating in continuous mode. A step size greater than 0 means that the slider is operating in discrete mode.

Related XML Attributes:

Returns
float

See also:

getThumbElevation

float getThumbElevation ()

Returns the elevation of the thumb.

Related XML Attributes:

Returns
float

getThumbRadius

int getThumbRadius ()

Returns the radius of the thumb.

Related XML Attributes:

Returns
int

getThumbTintList

ColorStateList getThumbTintList ()

Returns the color of the thumb.

Related XML Attributes:

Returns
ColorStateList

getTickActiveTintList

ColorStateList getTickActiveTintList ()

Returns the color of the ticks on the active portion of the track.

Related XML Attributes:

Returns
ColorStateList

getTickInactiveTintList

ColorStateList getTickInactiveTintList ()

Returns the color of the ticks on the inactive portion of the track.

Related XML Attributes:

Returns
ColorStateList

getTickTintList

ColorStateList getTickTintList ()

Returns the color of the tick if the active and inactive parts aren't different.

Related XML Attributes:

Returns
ColorStateList

Throws
IllegalStateException If tickColorActive and tickColorInactive have been set to different values.

getTrackActiveTintList

ColorStateList getTrackActiveTintList ()

Returns the color of the active portion of the track.

Related XML Attributes:

Returns
ColorStateList

getTrackHeight

int getTrackHeight ()

Returns the height of the track in pixels.

Related XML Attributes:

Returns
int

getTrackInactiveTintList

ColorStateList getTrackInactiveTintList ()

Returns the color of the inactive portion of the track.

Related XML Attributes:

Returns
ColorStateList

getTrackSidePadding

int getTrackSidePadding ()

Returns the side padding of the track.

Returns
int

getTrackTintList

ColorStateList getTrackTintList ()

Returns the color of the track if the active and inactive parts aren't different.

Related XML Attributes:

Returns
ColorStateList

Throws
IllegalStateException If trackColorActive and trackColorInactive have been set to different values.

getTrackWidth

int getTrackWidth ()

Returns the width of the track in pixels.

Returns
int

getValue

float getValue ()

Returns the value of the slider.

Related XML Attributes:

Returns
float

See also:

getValueFrom

float getValueFrom ()

Returns the slider's valueFrom value.

Related XML Attributes:

Returns
float

getValueTo

float getValueTo ()

Returns the slider's valueTo value.

Related XML Attributes:

Returns
float

See also:

hasLabelFormatter

boolean hasLabelFormatter ()

Returns true if the slider has a LabelFormatter attached, false otherwise.

Returns
boolean

onKeyDown

boolean onKeyDown (int keyCode, 
                KeyEvent event)

Parameters
keyCode int

event KeyEvent

Returns
boolean

onKeyUp

boolean onKeyUp (int keyCode, 
                KeyEvent event)

Parameters
keyCode int

event KeyEvent

Returns
boolean

onTouchEvent

boolean onTouchEvent (MotionEvent event)

Parameters
event MotionEvent

Returns
boolean

removeOnChangeListener

void removeOnChangeListener (L listener)

Removes a callback for value changes from this slider.

Parameters
listener L: The callback that'll stop receive slider changes

removeOnSliderTouchListener

void removeOnSliderTouchListener (T listener)

Removes a callback to be invoked when the slider touch event is being started or stopped

Parameters
listener T: The callback that'll stop be notified when the slider is being touched

setEnabled

void setEnabled (boolean enabled)

Parameters
enabled boolean

setFocusedThumbIndex

void setFocusedThumbIndex (int index)

Sets the index of the currently focused thumb

Parameters
index int

setHaloRadius

void setHaloRadius (int radius)

Sets the radius of the halo in pixels.

Related XML Attributes:

Parameters
radius int

See also:

setHaloRadiusResource

void setHaloRadiusResource (int radius)

Sets the radius of the halo from a dimension resource.

Related XML Attributes:

Parameters
radius int

See also:

setHaloTintList

void setHaloTintList (ColorStateList haloColor)

Sets the color of the halo.

Related XML Attributes:

Parameters
haloColor ColorStateList

See also:

setLabelBehavior

void setLabelBehavior (int labelBehavior)

Determines the ERROR(/LabelBehavior) used.

Related XML Attributes:

Parameters
labelBehavior int

setLabelFormatter

void setLabelFormatter (LabelFormatter formatter)

Registers a LabelFormatter to be used to format the value displayed in the bubble shown when the slider operates in discrete mode.

Parameters
formatter LabelFormatter: The LabelFormatter to use to format the bubble's text

setStepSize

void setStepSize (float stepSize)

Sets the step size to use to mark the ticks.

Setting this value to 0 will make the slider operate in continuous mode. Setting this value to a number greater than 0 will make the slider operate in discrete mode.

The step size must evenly divide the range described by the valueFrom and valueTo, it must be a factor of the range. If the step size is not a factor of the range, an IllegalStateException will be thrown when this view is laid out.

Setting this value to a negative value will result in an IllegalArgumentException.

Related XML Attributes:

Parameters
stepSize float: The interval value at which ticks must be drawn. Set to 0 to operate the slider in continuous mode and not have any ticks.

Throws
IllegalArgumentException If the step size is less than 0

See also:

setThumbElevation

void setThumbElevation (float elevation)

Sets the elevation of the thumb.

Related XML Attributes:

Parameters
elevation float

setThumbElevationResource

void setThumbElevationResource (int elevation)

Sets the elevation of the thumb from a dimension resource.

Related XML Attributes:

Parameters
elevation int

setThumbRadius

void setThumbRadius (int radius)

Sets the radius of the thumb in pixels.

Related XML Attributes:

Parameters
radius int

See also:

setThumbRadiusResource

void setThumbRadiusResource (int radius)

Sets the radius of the thumb from a dimension resource.

Related XML Attributes:

Parameters
radius int

See also:

setThumbTintList

void setThumbTintList (ColorStateList thumbColor)

Sets the color of the thumb.

Related XML Attributes:

Parameters
thumbColor ColorStateList

See also:

setTickActiveTintList

void setTickActiveTintList (ColorStateList tickColor)

Sets the color of the ticks on the active portion of the track.

Related XML Attributes:

Parameters
tickColor ColorStateList

setTickInactiveTintList

void setTickInactiveTintList (ColorStateList tickColor)

Sets the color of the ticks on the inactive portion of the track.

Related XML Attributes:

Parameters
tickColor ColorStateList

setTickTintList

void setTickTintList (ColorStateList tickColor)

Sets the color of the tick marks.

Related XML Attributes:

Parameters
tickColor ColorStateList

setTrackActiveTintList

void setTrackActiveTintList (ColorStateList trackColor)

Sets the color of the active portion of the track.

Related XML Attributes:

Parameters
trackColor ColorStateList

setTrackHeight

void setTrackHeight (int trackHeight)

Set the height of the track in pixels.

Related XML Attributes:

Parameters
trackHeight int

See also:

setTrackInactiveTintList

void setTrackInactiveTintList (ColorStateList trackColor)

Sets the color of the inactive portion of the track.

Related XML Attributes:

Parameters
trackColor ColorStateList

setTrackTintList

void setTrackTintList (ColorStateList trackColor)

Sets the color of the track.

Related XML Attributes:

Parameters
trackColor ColorStateList

setValue

void setValue (float value)

Sets the value of the slider.

The thumb value must be greater or equal to valueFrom, and lesser or equal to valueTo. If that is not the case, an IllegalStateException will be thrown when the view is laid out.

If the slider is in discrete mode (i.e. the tick increment value is greater than 0), the thumb's value must be set to a value falls on a tick (i.e.: value == valueFrom + x * stepSize, where x is an integer equal to or greater than 0). If that is not the case, an IllegalStateException will be thrown when the view is laid out.

Related XML Attributes:

Parameters
value float: The value to which to set the slider

See also:

setValueFrom

void setValueFrom (float valueFrom)

Sets the slider's valueFrom value.

The valueFrom value must be strictly lower than the valueTo value. If that is not the case, an IllegalStateException will be thrown when the view is laid out.

Related XML Attributes:

Parameters
valueFrom float: The minimum value for the slider's range of values

See also:

setValueTo

void setValueTo (float valueTo)

Sets the slider's valueTo value.

The valueTo value must be strictly greater than the valueFrom value. If that is not the case, an IllegalStateException will be thrown when the view is laid out.

Related XML Attributes:

Parameters
valueTo float: The maximum value for the slider's range of values

See also:

Protected methods

drawableStateChanged

void drawableStateChanged ()

onAttachedToWindow

void onAttachedToWindow ()

onDetachedFromWindow

void onDetachedFromWindow ()

onDraw

void onDraw (Canvas canvas)

Parameters
canvas Canvas

onFocusChanged

void onFocusChanged (boolean gainFocus, 
                int direction, 
                Rect previouslyFocusedRect)

Parameters
gainFocus boolean

direction int

previouslyFocusedRect Rect

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

Parcelable onSaveInstanceState ()

Returns
Parcelable

onSizeChanged

void onSizeChanged (int w, 
                int h, 
                int oldw, 
                int oldh)

Parameters
w int

h int

oldw int

oldh int

pickActiveThumb

boolean pickActiveThumb ()

Tries to pick the active thumb if one hasn't already been set. This will pick the closest thumb if there is only one thumb under the touch position. If there is more than one thumb under the touch position, it will wait for enough drag left or right to determine which thumb to pick.

Returns
boolean

setActiveThumbIndex

void setActiveThumbIndex (int index)

Parameters
index int