Slider

open class Slider : BaseSlider<S, L, T>

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

The slider can function either as a continuous slider, or as a discrete slider. The mode of operation is controlled by the value of the step size. If the step size is set to 0, the slider operates as a continuous slider where the slider's thumb can be moved to any position along the horizontal line. If the step size is set to a number greater than 0, the slider operates as a discrete slider where the slider's thumb will snap to the closest valid value. See * #setStepSize(float).

The interface defines a formatter to be used to render text within the value indicator label on interaction.

is a simple implementation of the that displays the selected value using letters to indicate magnitude (e.g.: 1.5K, 3M, 12B, etc..).

With the default style * com.google.android.material.R.style#Widget_MaterialComponents_Slider, colorPrimary and colorOnPrimary are used to customize the color of the slider when enabled, and colorOnSurface is used when disabled. The following attributes are used to customize the slider's appearance further:

  • haloColor}: the color of the halo around the thumb.
  • haloRadius}: The radius of the halo around the thumb.
  • labelBehavior}: The behavior of the label which can be LABEL_FLOATING}, LABEL_WITHIN_BOUNDS}, or LABEL_GONE}. See for more information.
  • labelStyle}: the style to apply to the value indicator .
  • thumbColor}: the color of the slider's thumb.
  • thumbStrokeColor}: the color of the thumb's stroke.
  • thumbStrokeWidth}: the width of the thumb's stroke.
  • thumbElevation}: the elevation of the slider's thumb.
  • thumbWidth}: The width of the slider's thumb.
  • thumbHeight}: The height of the slider's thumb.
  • thumbRadius}: The radius of the slider's thumb.
  • thumbTrackGapSize}: The size of the gap between the thumb and the track.
  • tickColorActive}: the color of the slider's tick marks for the active part of the track. Only used when the slider is in discrete mode.
  • tickColorInactive}: the color of the slider's tick marks for the inactive part of the track. Only used when the slider is in discrete mode.
  • tickColor}: the color of the slider's tick marks. Only used when the slider is in discrete mode. This is a short hand for setting both the tickColorActive} and *tickColorInactive to the same thing. This takes precedence over tickColorActive} and tickColorInactive}.
  • tickVisible}: Whether to show the tick marks. Only used when the slider is in discrete mode.
  • trackColorActive}: The color of the active part of the track.
  • trackColorInactive}: The color of the inactive part of the track.
  • trackColor}: The color of the whole track. This is a short hand for setting both the trackColorActive} and trackColorInactive} to the same thing. This takes precedence over trackColorActive} and trackColorInactive}.
  • trackHeight}: The height of the track.
  • trackInsideCornerSize}: The corner size on the inside of the track (visible with gap).
  • trackStopIndicatorSize}: The size of the stop indicator at the edges of the track.

The following XML attributes are used to set the slider's various parameters of operation:

  • android:valueFrom}: Required. The slider's minimum value. This attribute must be less than valueTo} or an will be thrown when the view is laid out.
  • android:valueTo}: Required. The slider's maximum value. This attribute must be greater than valueFrom} or an will be thrown when the view is laid out.
  • android:value}: Optional. The initial value of the slider. If not specified, the slider's minimum value android:valueFrom} is used.
  • android:stepSize}: Optional. This value dictates whether the slider operates in continuous mode, or in discrete mode. If missing or equal to 0, the slider operates in continuous mode. If greater than 0 and evenly divides the range described by *valueFrom and valueTo}, the slider operates in discrete mode. If negative an * is thrown, or if greater than 0 but not a factor of the range described by valueFrom} and valueTo}, an will be thrown when the view is laid out.

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

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

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int)

Types

Link copied to clipboard
Interface definition for a callback invoked when a slider's value is changed.
Link copied to clipboard
Interface definition for callbacks invoked when a slider's touch event is being started/stopped.

Functions

Link copied to clipboard
open fun getValue(): Float
Returns the value of the slider.
Link copied to clipboard
open fun setCustomThumbDrawable(@DrawableRes drawableResId: Int)
Sets the custom thumb drawable which will be used for all value positions.
Link copied to clipboard
open fun setValue(value: Float)
Sets the value of the slider.