diff --git a/catalog/java/io/material/catalog/slider/SliderDiscreteDemoFragment.java b/catalog/java/io/material/catalog/slider/SliderDiscreteDemoFragment.java index 542a4bba9b5..dfbb145f4eb 100644 --- a/catalog/java/io/material/catalog/slider/SliderDiscreteDemoFragment.java +++ b/catalog/java/io/material/catalog/slider/SliderDiscreteDemoFragment.java @@ -48,6 +48,7 @@ public View onCreateDemoView( setUpSlider(view, R.id.switch_button_3, R.id.slider_3, null); setUpSlider(view, R.id.switch_button_4, R.id.slider_4, new BasicLabelFormatter()); setUpSlider(view, R.id.switch_button_5, R.id.slider_5, null); + setUpSlider(view, R.id.switch_button_6, R.id.slider_6, null); return view; } diff --git a/catalog/java/io/material/catalog/slider/res/layout/cat_slider_demo_discrete.xml b/catalog/java/io/material/catalog/slider/res/layout/cat_slider_demo_discrete.xml index 766a12e981a..1df0a5ad35f 100644 --- a/catalog/java/io/material/catalog/slider/res/layout/cat_slider_demo_discrete.xml +++ b/catalog/java/io/material/catalog/slider/res/layout/cat_slider_demo_discrete.xml @@ -16,6 +16,7 @@ --> @@ -175,5 +176,36 @@ android:valueTo="5.75" android:stepSize="0.25" /> + + + + + + + + + diff --git a/catalog/java/io/material/catalog/slider/res/values/strings.xml b/catalog/java/io/material/catalog/slider/res/values/strings.xml index 4b52b10b41c..ee690ed23a0 100644 --- a/catalog/java/io/material/catalog/slider/res/values/strings.xml +++ b/catalog/java/io/material/catalog/slider/res/values/strings.xml @@ -34,6 +34,7 @@ Negative numbers! With a label formatter I can have decimal numbers? + Without tick marks Slider started being touched Slider stopped being touched diff --git a/lib/java/com/google/android/material/slider/BaseSlider.java b/lib/java/com/google/android/material/slider/BaseSlider.java index 8b81450f2c4..964c3a47cb7 100644 --- a/lib/java/com/google/android/material/slider/BaseSlider.java +++ b/lib/java/com/google/android/material/slider/BaseSlider.java @@ -125,6 +125,8 @@ * discrete mode. This is a short hand for setting both the {@code tickColorActive} and {@code * tickColorInactive} to the same thing. This takes precedence over {@code tickColorActive} * and {@code tickColorInactive}. + *
  • {@code tickVisible}: Whether to show the tick marks. Only used when the slider is in + * discrete mode. *
  • {@code trackColorActive}: The color of the active part of the track. *
  • {@code trackColorInactive}: The color of the inactive part of the track. *
  • {@code trackColor}: The color of the whole track. This is a short hand for setting both the @@ -167,6 +169,7 @@ * @attr ref com.google.android.material.R.styleable#Slider_tickColor * @attr ref com.google.android.material.R.styleable#Slider_tickColorActive * @attr ref com.google.android.material.R.styleable#Slider_tickColorInactive + * @attr ref com.google.android.material.R.styleable#Slider_tickVisible * @attr ref com.google.android.material.R.styleable#Slider_trackColor * @attr ref com.google.android.material.R.styleable#Slider_trackColorActive * @attr ref com.google.android.material.R.styleable#Slider_trackColorInactive @@ -244,6 +247,7 @@ private interface TooltipDrawableFactory { private int focusedThumbIdx = -1; private float stepSize = 0.0f; private float[] ticksCoordinates; + private boolean tickVisible = true; private int trackWidth; private boolean forceDrawCompatHalo; private boolean isLongPress = false; @@ -401,6 +405,7 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle ? haloColor : AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color)); + tickVisible = a.getBoolean(R.styleable.Slider_tickVisible, true); boolean hasTickColor = a.hasValue(R.styleable.Slider_tickColor); int tickColorInactiveRes = hasTickColor ? R.styleable.Slider_tickColor : R.styleable.Slider_tickColorInactive; @@ -1119,6 +1124,29 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) { invalidate(); } + /** + * Returns whether the tick marks are visible. Only used when the slider is in discrete mode. + * + * @see #setTickVisible(boolean) + * @attr ref com.google.android.material.R.styleable#Slider_tickVisible + */ + public boolean isTickVisible() { + return tickVisible; + } + + /** + * Sets whether the tick marks are visible. Only used when the slider is in discrete mode. + * + * @param tickVisible The visibility of tick marks. + * @attr ref com.google.android.material.R.styleable#Slider_tickVisible + */ + public void setTickVisible(boolean tickVisible) { + if (this.tickVisible != tickVisible){ + this.tickVisible = tickVisible; + postInvalidate(); + } + } + /** * Returns the color of the track if the active and inactive parts aren't different. * @@ -1270,14 +1298,16 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { trackWidth = Math.max(w - trackSidePadding * 2, 0); // Update the visible tick coordinates. - if (stepSize > 0.0f) { - calculateTicksCoordinates(); - } + maybeCalculateTicksCoordinates(); updateHaloHotspot(); } - private void calculateTicksCoordinates() { + private void maybeCalculateTicksCoordinates() { + if (stepSize <= 0.0f) { + return; + } + validateConfigurationIfDirty(); int tickCount = (int) ((valueTo - valueFrom) / stepSize + 1); @@ -1318,9 +1348,7 @@ protected void onDraw(@NonNull Canvas canvas) { validateConfigurationIfDirty(); // Update the visible tick coordinates. - if (stepSize > 0.0f) { - calculateTicksCoordinates(); - } + maybeCalculateTicksCoordinates(); } super.onDraw(canvas); @@ -1332,9 +1360,7 @@ protected void onDraw(@NonNull Canvas canvas) { drawActiveTrack(canvas, trackWidth, top); } - if (stepSize > 0.0f) { - drawTicks(canvas); - } + maybeDrawTicks(canvas); if ((thumbIsPressed || isFocused()) && isEnabled()) { maybeDrawHalo(canvas, trackWidth, top); @@ -1395,7 +1421,11 @@ private void drawActiveTrack(@NonNull Canvas canvas, int width, int top) { canvas.drawLine(left, top, right, top, activeTrackPaint); } - private void drawTicks(@NonNull Canvas canvas) { + private void maybeDrawTicks(@NonNull Canvas canvas) { + if (!tickVisible || stepSize <= 0.0f) { + return; + } + float[] activeRange = getActiveRange(); int leftPivotIndex = pivotIndex(ticksCoordinates, activeRange[0]); int rightPivotIndex = pivotIndex(ticksCoordinates, activeRange[1]); 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 a354cfcd97f..34f7e7ca076 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 @@ -27,6 +27,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 88538d520ef..e63c50e5c9f 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 @@ -51,6 +51,8 @@ + +