Skip to content

Commit 9c1c0de

Browse files
committed
[Slider] Add API to set thumb ripple effect color
1 parent 7ae12b5 commit 9c1c0de

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
@@ -330,21 +330,24 @@ thickness.
330330

331331
#### Thumb attributes
332332

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

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

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

350353
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
@@ -66,6 +66,7 @@
6666
import android.os.Bundle;
6767
import android.os.Parcel;
6868
import android.os.Parcelable;
69+
import androidx.annotation.RequiresApi;
6970
import androidx.appcompat.content.res.AppCompatResources;
7071
import android.text.TextUtils;
7172
import android.util.AttributeSet;
@@ -271,6 +272,7 @@ abstract class BaseSlider<
271272
private static final float THUMB_WIDTH_PRESSED_RATIO = .5f;
272273
private static final int TRACK_CORNER_SIZE_UNSET = -1;
273274
private static final float TOUCH_SLOP_RATIO = .8f;
275+
private static final int DEFAULT_HALO_EFFECT_COLOR = 0x8dffffff;
274276

275277
static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_Slider;
276278
static final int UNIT_VALUE = 1;
@@ -383,6 +385,7 @@ abstract class BaseSlider<
383385
private boolean dirtyConfig;
384386

385387
@NonNull private ColorStateList haloColor;
388+
@NonNull private ColorStateList haloEffectColor;
386389
@NonNull private ColorStateList tickColorActive;
387390
@NonNull private ColorStateList tickColorInactive;
388391
@NonNull private ColorStateList trackColorActive;
@@ -592,6 +595,15 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
592595
? haloColor
593596
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
594597

598+
if (VERSION.SDK_INT >= VERSION_CODES.S) {
599+
ColorStateList haloEffectColor =
600+
MaterialResources.getColorStateList(context, a, R.styleable.Slider_haloEffectColor);
601+
setHaloEffectTintList(
602+
haloEffectColor != null
603+
? haloEffectColor
604+
: ColorStateList.valueOf(DEFAULT_HALO_EFFECT_COLOR));
605+
}
606+
595607
tickVisibilityMode =
596608
a.hasValue(R.styleable.Slider_tickVisibilityMode)
597609
? a.getInt(R.styleable.Slider_tickVisibilityMode, -1)
@@ -1665,6 +1677,39 @@ public void setHaloTintList(@NonNull ColorStateList haloColor) {
16651677
invalidate();
16661678
}
16671679

1680+
/**
1681+
* Returns the effect color of the halo.
1682+
*
1683+
* @see #setHaloEffectTintList(ColorStateList)
1684+
* @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor
1685+
*/
1686+
@RequiresApi(api = VERSION_CODES.S)
1687+
@NonNull
1688+
public ColorStateList getHaloEffectTintList() {
1689+
return haloEffectColor;
1690+
}
1691+
1692+
/**
1693+
* Sets the effect color of the halo.
1694+
*
1695+
* @see #getHaloEffectTintList()
1696+
* @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor
1697+
*/
1698+
@RequiresApi(api = VERSION_CODES.S)
1699+
public void setHaloEffectTintList(@NonNull ColorStateList haloEffectColor) {
1700+
if (haloEffectColor.equals(this.haloEffectColor)) {
1701+
return;
1702+
}
1703+
1704+
this.haloEffectColor = haloEffectColor;
1705+
1706+
final Drawable background = getBackground();
1707+
if (!shouldDrawCompatHalo() && background instanceof RippleDrawable) {
1708+
((RippleDrawable) background).setEffectColor(haloEffectColor);
1709+
postInvalidate();
1710+
}
1711+
}
1712+
16681713
/**
16691714
* Returns the color of the thumb.
16701715
*

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

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<public name="centered" type="attr" />
2121
<public name="haloColor" type="attr" />
22+
<public name="haloEffectColor" type="attr" />
2223
<public name="haloRadius" type="attr" />
2324
<public name="labelBehavior" type="attr" />
2425
<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
@@ -26,6 +26,9 @@
2626
<attr name="centered" format="boolean" />
2727
<!-- The color of the slider's halo. -->
2828
<attr name="haloColor" format="color" />
29+
<!-- The color of the slider's halo effect. The attribute only has an effect on API 31
30+
and above. -->
31+
<attr name="haloEffectColor" format="color" />
2932
<!-- The radius of the halo. -->
3033
<attr name="haloRadius" format="dimension" />
3134
<!-- 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
@@ -55,6 +55,7 @@
5555

5656
<style name="Widget.Material3.Slider" parent="Widget.MaterialComponents.Slider">
5757
<item name="haloColor">#00FFFFFF</item>
58+
<item name="haloEffectColor">@android:color/transparent</item>
5859
<item name="labelStyle">@style/Widget.Material3.Slider.Label</item>
5960
<item name="thumbColor">@color/m3_slider_thumb_color</item>
6061
<item name="tickColorActive">@color/m3_slider_active_tick_marks_color</item>

0 commit comments

Comments
 (0)