Skip to content

Commit ef40210

Browse files
committed
[Slider] Add API to set thumb ripple effect color
1 parent 95025c6 commit ef40210

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

docs/components/Slider.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -329,21 +329,24 @@ thickness.
329329

330330
#### Thumb attributes
331331

332-
| Element | Attribute | Related method(s) | Default value |
333-
|------------------|-------------------------|-----------------------------------------------------------------------------------|------------------------------|
334-
| **Color** | `app:thumbColor` | `setThumbTintList`<br/>`getThumbTintList` | `?attr/colorPrimary` |
335-
| **Width** | `app:thumbWidth` | `setThumbWidth`<br/>`setThumbWidthResource`<br/>`getThumbWidth` | `4dp` |
336-
| **Height** | `app:thumbHeight` | `setThumbHeight`<br/>`setThumbHeightResource`<br/>`getThumbHeight` | `44dp` |
337-
| **Radius** | `app:thumbRadius` | `setThumbRadiusResource`<br/>`setThumbRadius`<br/>`getThumbRadius` | N/A |
338-
| **Elevation** | `app:thumbElevation` | `setThumbElevationResource`<br/>`setThumbElevation`<br/>`getThumbElevation` | `2dp` |
339-
| **Halo color** | `app:haloColor` | `setHaloTintList`<br/>`getHaloTintList` | `@android:color/transparent` |
340-
| **Halo radius** | `app:haloRadius` | `setHaloRadiusResource`<br/>`setHaloRadius`<br/>`getHaloRadius` | N/A |
341-
| **Stroke color** | `app:thumbStrokeColor` | `setThumbStrokeColor`<br/>`setThumbStrokeColorResource`<br/>`getThumbStrokeColor` | `null` |
342-
| **Stroke width** | `app:thumbStrokeWidth` | `setThumbStrokeWidth`<br/>`setThumbStrokeWidthResource`<br/>`getThumbStrokeWidth` | `0dp` |
343-
| **Gap size** | `app:thumbTrackGapSize` | `setThumbTrackGapSize`<br/>`getThumbTrackGapSize` | `6dp` |
332+
| Element | Attribute | Related method(s) | Default value |
333+
|-----------------------|-------------------------|-----------------------------------------------------------------------------------|------------------------------|
334+
| **Color** | `app:thumbColor` | `setThumbTintList`<br/>`getThumbTintList` | `?attr/colorPrimary` |
335+
| **Width** | `app:thumbWidth` | `setThumbWidth`<br/>`setThumbWidthResource`<br/>`getThumbWidth` | `4dp` |
336+
| **Height** | `app:thumbHeight` | `setThumbHeight`<br/>`setThumbHeightResource`<br/>`getThumbHeight` | `44dp` |
337+
| **Radius** | `app:thumbRadius` | `setThumbRadiusResource`<br/>`setThumbRadius`<br/>`getThumbRadius` | N/A |
338+
| **Elevation** | `app:thumbElevation` | `setThumbElevationResource`<br/>`setThumbElevation`<br/>`getThumbElevation` | `2dp` |
339+
| **Halo color** | `app:haloColor` | `setHaloTintList`<br/>`getHaloTintList` | `@android:color/transparent` |
340+
| **Halo effect color** | `app:haloEffectColor` | `setHaloEffectTintList`<br/>`getHaloEffectTintList` | `@android:color/transparent` |
341+
| **Halo radius** | `app:haloRadius` | `setHaloRadiusResource`<br/>`setHaloRadius`<br/>`getHaloRadius` | N/A |
342+
| **Stroke color** | `app:thumbStrokeColor` | `setThumbStrokeColor`<br/>`setThumbStrokeColorResource`<br/>`getThumbStrokeColor` | `null` |
343+
| **Stroke width** | `app:thumbStrokeWidth` | `setThumbStrokeWidth`<br/>`setThumbStrokeWidthResource`<br/>`getThumbStrokeWidth` | `0dp` |
344+
| **Gap size** | `app:thumbTrackGapSize` | `setThumbTrackGapSize`<br/>`getThumbTrackGapSize` | `6dp` |
344345

345346
**Note:** `app:thumbWidth` and `app:thumbHeight` take precedence over `app:thumbRadius`.
346347

348+
**Note:** `app:haloEffectColor` only has an effect on API 31 and above.
349+
347350
#### Value label attributes
348351

349352
Element | Attribute | Related method(s) | Default value

lib/java/com/google/android/material/slider/BaseSlider.java

+45
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import android.os.Bundle;
6464
import android.os.Parcel;
6565
import android.os.Parcelable;
66+
import androidx.annotation.RequiresApi;
6667
import androidx.appcompat.content.res.AppCompatResources;
6768
import android.util.AttributeSet;
6869
import android.util.Log;
@@ -265,6 +266,7 @@ abstract class BaseSlider<
265266
private static final double THRESHOLD = .0001;
266267
private static final float THUMB_WIDTH_PRESSED_RATIO = .5f;
267268
private static final int TRACK_CORNER_SIZE_UNSET = -1;
269+
private static final int DEFAULT_HALO_EFFECT_COLOR = 0x8dffffff;
268270

269271
static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_Slider;
270272
static final int UNIT_VALUE = 1;
@@ -361,6 +363,7 @@ abstract class BaseSlider<
361363
private boolean dirtyConfig;
362364

363365
@NonNull private ColorStateList haloColor;
366+
@NonNull private ColorStateList haloEffectColor;
364367
@NonNull private ColorStateList tickColorActive;
365368
@NonNull private ColorStateList tickColorInactive;
366369
@NonNull private ColorStateList trackColorActive;
@@ -566,6 +569,15 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
566569
? haloColor
567570
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
568571

572+
if (VERSION.SDK_INT >= VERSION_CODES.S) {
573+
ColorStateList haloEffectColor =
574+
MaterialResources.getColorStateList(context, a, R.styleable.Slider_haloEffectColor);
575+
setHaloEffectTintList(
576+
haloEffectColor != null
577+
? haloEffectColor
578+
: ColorStateList.valueOf(DEFAULT_HALO_EFFECT_COLOR));
579+
}
580+
569581
tickVisible = a.getBoolean(R.styleable.Slider_tickVisible, true);
570582
boolean hasTickColor = a.hasValue(R.styleable.Slider_tickColor);
571583
int tickColorInactiveRes =
@@ -1646,6 +1658,39 @@ public void setHaloTintList(@NonNull ColorStateList haloColor) {
16461658
invalidate();
16471659
}
16481660

1661+
/**
1662+
* Returns the effect color of the halo.
1663+
*
1664+
* @see #setHaloEffectTintList(ColorStateList)
1665+
* @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor
1666+
*/
1667+
@RequiresApi(api = VERSION_CODES.S)
1668+
@NonNull
1669+
public ColorStateList getHaloEffectTintList() {
1670+
return haloEffectColor;
1671+
}
1672+
1673+
/**
1674+
* Sets the effect color of the halo.
1675+
*
1676+
* @see #getHaloEffectTintList()
1677+
* @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor
1678+
*/
1679+
@RequiresApi(api = VERSION_CODES.S)
1680+
public void setHaloEffectTintList(@NonNull ColorStateList haloEffectColor) {
1681+
if (haloEffectColor.equals(this.haloEffectColor)) {
1682+
return;
1683+
}
1684+
1685+
this.haloEffectColor = haloEffectColor;
1686+
1687+
final Drawable background = getBackground();
1688+
if (!shouldDrawCompatHalo() && background instanceof RippleDrawable) {
1689+
((RippleDrawable) background).setEffectColor(haloEffectColor);
1690+
postInvalidate();
1691+
}
1692+
}
1693+
16491694
/**
16501695
* Returns the color of the thumb.
16511696
*

lib/java/com/google/android/material/slider/res-public/values/public.xml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<public name="sliderStyle" type="attr"/>
1919

2020
<public name="haloColor" type="attr" />
21+
<public name="haloEffectColor" type="attr" />
2122
<public name="haloRadius" type="attr" />
2223
<public name="labelBehavior" type="attr" />
2324
<public name="labelStyle" type="attr" />

lib/java/com/google/android/material/slider/res/values/attrs.xml

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
<attr name="android:orientation" />
2626
<!-- The color of the slider's halo. -->
2727
<attr name="haloColor" format="color" />
28+
<!-- The color of the slider's halo effect. The attribute only has an effect on API 31
29+
and above. -->
30+
<attr name="haloEffectColor" format="color" />
2831
<!-- The radius of the halo. -->
2932
<attr name="haloRadius" format="dimension" />
3033
<!-- Determines if Slider should increase its default height to include space for the label. -->

lib/java/com/google/android/material/slider/res/values/styles.xml

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
<style name="Widget.Material3.Slider" parent="Widget.MaterialComponents.Slider">
6363
<item name="haloColor">#00FFFFFF</item>
64+
<item name="haloEffectColor">@android:color/transparent</item>
6465
<item name="labelStyle">@style/Widget.Material3.Slider.Label</item>
6566
<item name="thumbColor">@color/m3_slider_thumb_color</item>
6667
<item name="tickColorActive">@color/m3_slider_active_tick_marks_color</item>

0 commit comments

Comments
 (0)