Skip to content

Commit 03f3033

Browse files
committed
[Slider] Add API to set thumb ripple effect color
^ Conflicts: ^ lib/java/com/google/android/material/slider/BaseSlider.java
1 parent 79bd7d7 commit 03f3033

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
@@ -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.util.AttributeSet;
7172
import android.util.Log;
@@ -270,6 +271,7 @@ abstract class BaseSlider<
270271
private static final float THUMB_WIDTH_PRESSED_RATIO = .5f;
271272
private static final int TRACK_CORNER_SIZE_UNSET = -1;
272273
private static final float TOUCH_SLOP_RATIO = .8f;
274+
private static final int DEFAULT_HALO_EFFECT_COLOR = 0x8dffffff;
273275

274276
static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_Slider;
275277
static final int UNIT_VALUE = 1;
@@ -381,6 +383,7 @@ abstract class BaseSlider<
381383
private boolean dirtyConfig;
382384

383385
@NonNull private ColorStateList haloColor;
386+
@NonNull private ColorStateList haloEffectColor;
384387
@NonNull private ColorStateList tickColorActive;
385388
@NonNull private ColorStateList tickColorInactive;
386389
@NonNull private ColorStateList trackColorActive;
@@ -589,6 +592,15 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
589592
? haloColor
590593
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
591594

595+
if (VERSION.SDK_INT >= VERSION_CODES.S) {
596+
ColorStateList haloEffectColor =
597+
MaterialResources.getColorStateList(context, a, R.styleable.Slider_haloEffectColor);
598+
setHaloEffectTintList(
599+
haloEffectColor != null
600+
? haloEffectColor
601+
: ColorStateList.valueOf(DEFAULT_HALO_EFFECT_COLOR));
602+
}
603+
592604
tickVisibilityMode =
593605
a.hasValue(R.styleable.Slider_tickVisibilityMode)
594606
? a.getInt(R.styleable.Slider_tickVisibilityMode, -1)
@@ -1662,6 +1674,39 @@ public void setHaloTintList(@NonNull ColorStateList haloColor) {
16621674
invalidate();
16631675
}
16641676

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

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)