diff --git a/docs/components/Slider.md b/docs/components/Slider.md index dc194cb0b1e..b1a99127418 100644 --- a/docs/components/Slider.md +++ b/docs/components/Slider.md @@ -329,21 +329,24 @@ thickness. #### Thumb attributes -| Element | Attribute | Related method(s) | Default value | -|------------------|-------------------------|-----------------------------------------------------------------------------------|------------------------------| -| **Color** | `app:thumbColor` | `setThumbTintList`
`getThumbTintList` | `?attr/colorPrimary` | -| **Width** | `app:thumbWidth` | `setThumbWidth`
`setThumbWidthResource`
`getThumbWidth` | `4dp` | -| **Height** | `app:thumbHeight` | `setThumbHeight`
`setThumbHeightResource`
`getThumbHeight` | `44dp` | -| **Radius** | `app:thumbRadius` | `setThumbRadiusResource`
`setThumbRadius`
`getThumbRadius` | N/A | -| **Elevation** | `app:thumbElevation` | `setThumbElevationResource`
`setThumbElevation`
`getThumbElevation` | `2dp` | -| **Halo color** | `app:haloColor` | `setHaloTintList`
`getHaloTintList` | `@android:color/transparent` | -| **Halo radius** | `app:haloRadius` | `setHaloRadiusResource`
`setHaloRadius`
`getHaloRadius` | N/A | -| **Stroke color** | `app:thumbStrokeColor` | `setThumbStrokeColor`
`setThumbStrokeColorResource`
`getThumbStrokeColor` | `null` | -| **Stroke width** | `app:thumbStrokeWidth` | `setThumbStrokeWidth`
`setThumbStrokeWidthResource`
`getThumbStrokeWidth` | `0dp` | -| **Gap size** | `app:thumbTrackGapSize` | `setThumbTrackGapSize`
`getThumbTrackGapSize` | `6dp` | +| Element | Attribute | Related method(s) | Default value | +|-----------------------|-------------------------|-----------------------------------------------------------------------------------|------------------------------| +| **Color** | `app:thumbColor` | `setThumbTintList`
`getThumbTintList` | `?attr/colorPrimary` | +| **Width** | `app:thumbWidth` | `setThumbWidth`
`setThumbWidthResource`
`getThumbWidth` | `4dp` | +| **Height** | `app:thumbHeight` | `setThumbHeight`
`setThumbHeightResource`
`getThumbHeight` | `44dp` | +| **Radius** | `app:thumbRadius` | `setThumbRadiusResource`
`setThumbRadius`
`getThumbRadius` | N/A | +| **Elevation** | `app:thumbElevation` | `setThumbElevationResource`
`setThumbElevation`
`getThumbElevation` | `2dp` | +| **Halo color** | `app:haloColor` | `setHaloTintList`
`getHaloTintList` | `@android:color/transparent` | +| **Halo effect color** | `app:haloEffectColor` | `setHaloEffectTintList`
`getHaloEffectTintList` | `@android:color/transparent` | +| **Halo radius** | `app:haloRadius` | `setHaloRadiusResource`
`setHaloRadius`
`getHaloRadius` | N/A | +| **Stroke color** | `app:thumbStrokeColor` | `setThumbStrokeColor`
`setThumbStrokeColorResource`
`getThumbStrokeColor` | `null` | +| **Stroke width** | `app:thumbStrokeWidth` | `setThumbStrokeWidth`
`setThumbStrokeWidthResource`
`getThumbStrokeWidth` | `0dp` | +| **Gap size** | `app:thumbTrackGapSize` | `setThumbTrackGapSize`
`getThumbTrackGapSize` | `6dp` | **Note:** `app:thumbWidth` and `app:thumbHeight` take precedence over `app:thumbRadius`. +**Note:** `app:haloEffectColor` only has an effect on API 31 and above. + #### Value label attributes Element | Attribute | Related method(s) | Default value diff --git a/lib/java/com/google/android/material/slider/BaseSlider.java b/lib/java/com/google/android/material/slider/BaseSlider.java index 6212302bca6..34a3ae31e75 100644 --- a/lib/java/com/google/android/material/slider/BaseSlider.java +++ b/lib/java/com/google/android/material/slider/BaseSlider.java @@ -63,6 +63,7 @@ import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; +import androidx.annotation.RequiresApi; import androidx.appcompat.content.res.AppCompatResources; import android.util.AttributeSet; import android.util.Log; @@ -265,6 +266,7 @@ abstract class BaseSlider< private static final double THRESHOLD = .0001; private static final float THUMB_WIDTH_PRESSED_RATIO = .5f; private static final int TRACK_CORNER_SIZE_UNSET = -1; + private static final int DEFAULT_HALO_EFFECT_COLOR = 0x8dffffff; static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_Slider; static final int UNIT_VALUE = 1; @@ -361,6 +363,7 @@ abstract class BaseSlider< private boolean dirtyConfig; @NonNull private ColorStateList haloColor; + @NonNull private ColorStateList haloEffectColor; @NonNull private ColorStateList tickColorActive; @NonNull private ColorStateList tickColorInactive; @NonNull private ColorStateList trackColorActive; @@ -566,6 +569,15 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle ? haloColor : AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color)); + if (VERSION.SDK_INT >= VERSION_CODES.S) { + ColorStateList haloEffectColor = + MaterialResources.getColorStateList(context, a, R.styleable.Slider_haloEffectColor); + setHaloEffectTintList( + haloEffectColor != null + ? haloEffectColor + : ColorStateList.valueOf(DEFAULT_HALO_EFFECT_COLOR)); + } + tickVisible = a.getBoolean(R.styleable.Slider_tickVisible, true); boolean hasTickColor = a.hasValue(R.styleable.Slider_tickColor); int tickColorInactiveRes = @@ -1646,6 +1658,39 @@ public void setHaloTintList(@NonNull ColorStateList haloColor) { invalidate(); } + /** + * Returns the effect color of the halo. + * + * @see #setHaloEffectTintList(ColorStateList) + * @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor + */ + @RequiresApi(api = VERSION_CODES.S) + @NonNull + public ColorStateList getHaloEffectTintList() { + return haloEffectColor; + } + + /** + * Sets the effect color of the halo. + * + * @see #getHaloEffectTintList() + * @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor + */ + @RequiresApi(api = VERSION_CODES.S) + public void setHaloEffectTintList(@NonNull ColorStateList haloEffectColor) { + if (haloEffectColor.equals(this.haloEffectColor)) { + return; + } + + this.haloEffectColor = haloEffectColor; + + final Drawable background = getBackground(); + if (!shouldDrawCompatHalo() && background instanceof RippleDrawable) { + ((RippleDrawable) background).setEffectColor(haloEffectColor); + postInvalidate(); + } + } + /** * Returns the color of the thumb. * diff --git a/lib/java/com/google/android/material/slider/res-public/values/public.xml b/lib/java/com/google/android/material/slider/res-public/values/public.xml index 9401e49eca3..0c0ae28de61 100644 --- a/lib/java/com/google/android/material/slider/res-public/values/public.xml +++ b/lib/java/com/google/android/material/slider/res-public/values/public.xml @@ -18,6 +18,7 @@ + diff --git a/lib/java/com/google/android/material/slider/res/values/attrs.xml b/lib/java/com/google/android/material/slider/res/values/attrs.xml index 6fb576b1517..9f4f820d15b 100644 --- a/lib/java/com/google/android/material/slider/res/values/attrs.xml +++ b/lib/java/com/google/android/material/slider/res/values/attrs.xml @@ -25,6 +25,9 @@ + + diff --git a/lib/java/com/google/android/material/slider/res/values/styles.xml b/lib/java/com/google/android/material/slider/res/values/styles.xml index 9195016f58d..bd97ed4c1b1 100644 --- a/lib/java/com/google/android/material/slider/res/values/styles.xml +++ b/lib/java/com/google/android/material/slider/res/values/styles.xml @@ -61,6 +61,7 @@