Skip to content

Commit f3dd354

Browse files
committed
[Slider] Add new tick visibility modes
1 parent 8f860a2 commit f3dd354

File tree

7 files changed

+188
-25
lines changed

7 files changed

+188
-25
lines changed

catalog/java/io/material/catalog/slider/res/layout/cat_slider_demo_discrete.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
android:valueFrom="0"
164164
android:valueTo="10"
165165
android:stepSize="1"
166-
app:tickVisible="false"/>
166+
app:tickVisibilityMode="hidden" />
167167
</LinearLayout>
168168

169169
<com.google.android.material.materialswitch.MaterialSwitch

docs/components/Slider.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,15 @@ Element | Attribute | Related method(s)
364364
| **Color for tick's inactive part** | `app:tickColorInactive` | `setTickInactiveTintList`<br/>`getTickInactiveTintList` | `?attr/colorPrimary` |
365365
| **Radius for tick's active part** | `app:tickRadiusActive` | `setTickActiveRadius`<br/>`getTickActiveRadius` | `null` (1/2 trackStopIndicatorSize) |
366366
| **Radius for tick's inactive part** | `app:tickRadiusInactive` | `setTickInactiveRadius`<br/>`getTickInactiveRadius` | `null` (1/2 trackStopIndicatorSize) |
367-
| **Tick visible** | `app:tickVisible` | `setTickVisible`<br/>`isTickVisible()` | `true` |
367+
| **Tick visible** (deprecated) | `app:tickVisible` | `setTickVisible`<br/>`isTickVisible()` | `true` |
368+
| **Tick visibility mode** | `app:tickVisibilityMode` | `setTickVisibilityMode`<br/>`getTickVisibilityMode()` | `autoLimit` |
368369

369370
**Note:** `app:tickColor` takes precedence over `app:tickColorActive` and
370-
`app:tickColorInative`. It's a shorthand for setting both values to the same
371+
`app:tickColorInactive`. It's a shorthand for setting both values to the same
371372
thing.
372373

374+
**Note:** `app:tickVisible` is deprecated in favor of `app:tickVisibilityMode`.
375+
373376
#### Styles
374377

375378
Element | Style

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

Lines changed: 102 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import static com.google.android.material.slider.LabelFormatter.LABEL_WITHIN_BOUNDS;
3030
import static com.google.android.material.slider.SliderOrientation.HORIZONTAL;
3131
import static com.google.android.material.slider.SliderOrientation.VERTICAL;
32+
import static com.google.android.material.slider.TickVisibilityMode.TICK_VISIBILITY_AUTO_HIDE;
33+
import static com.google.android.material.slider.TickVisibilityMode.TICK_VISIBILITY_AUTO_LIMIT;
34+
import static com.google.android.material.slider.TickVisibilityMode.TICK_VISIBILITY_HIDDEN;
35+
import static com.google.android.material.slider.TickVisibilityMode.TICK_VISIBILITY_VISIBLE_ALL;
3236
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
3337
import static java.lang.Float.compare;
3438
import static java.lang.Math.abs;
@@ -157,8 +161,10 @@
157161
* discrete mode. This is a short hand for setting both the {@code tickColorActive} and {@code
158162
* tickColorInactive} to the same thing. This takes precedence over {@code tickColorActive}
159163
* and {@code tickColorInactive}.
160-
* <li>{@code tickVisible}: Whether to show the tick marks. Only used when the slider is in
161-
* discrete mode.
164+
* <li>{@code tickVisible} (<b>deprecated</b>, use {@code tickVisibilityMode} instead):
165+
* Whether to show the tick marks. Only used when the slider is in discrete mode.
166+
* <li>{@code tickVisibilityMode}: Mode to specify the visibility of tick marks. Only used when
167+
* the slider is in discrete mode.
162168
* <li>{@code trackColorActive}: The color of the active part of the track.
163169
* <li>{@code trackColorInactive}: The color of the inactive part of the track.
164170
* <li>{@code trackColor}: The color of the whole track. This is a short hand for setting both the
@@ -210,6 +216,7 @@
210216
* @attr ref com.google.android.material.R.styleable#Slider_tickColorActive
211217
* @attr ref com.google.android.material.R.styleable#Slider_tickColorInactive
212218
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
219+
* @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
213220
* @attr ref com.google.android.material.R.styleable#Slider_trackColor
214221
* @attr ref com.google.android.material.R.styleable#Slider_trackColorActive
215222
* @attr ref com.google.android.material.R.styleable#Slider_trackColorInactive
@@ -352,7 +359,7 @@ abstract class BaseSlider<
352359
private int focusedThumbIdx = -1;
353360
private float stepSize = 0.0f;
354361
private float[] ticksCoordinates;
355-
private boolean tickVisible = true;
362+
private int tickVisibilityMode;
356363
private int tickActiveRadius;
357364
private int tickInactiveRadius;
358365
private int trackWidth;
@@ -566,7 +573,10 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
566573
? haloColor
567574
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
568575

569-
tickVisible = a.getBoolean(R.styleable.Slider_tickVisible, true);
576+
tickVisibilityMode = a.hasValue(R.styleable.Slider_tickVisibilityMode)
577+
? a.getInt(R.styleable.Slider_tickVisibilityMode, -1)
578+
: convertToTickVisibilityMode(a.getBoolean(R.styleable.Slider_tickVisible, true));
579+
570580
boolean hasTickColor = a.hasValue(R.styleable.Slider_tickColor);
571581
int tickColorInactiveRes =
572582
hasTickColor ? R.styleable.Slider_tickColor : R.styleable.Slider_tickColorInactive;
@@ -1768,22 +1778,61 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
17681778
/**
17691779
* Returns whether the tick marks are visible. Only used when the slider is in discrete mode.
17701780
*
1771-
* @see #setTickVisible(boolean)
17721781
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
17731782
*/
17741783
public boolean isTickVisible() {
1775-
return tickVisible;
1784+
switch (tickVisibilityMode) {
1785+
case TICK_VISIBILITY_VISIBLE_ALL:
1786+
case TICK_VISIBILITY_AUTO_LIMIT:
1787+
return true;
1788+
case TICK_VISIBILITY_AUTO_HIDE:
1789+
return getDesiredTickCount() <= getMaxTickCount();
1790+
case TICK_VISIBILITY_HIDDEN:
1791+
return false;
1792+
default:
1793+
throw new RuntimeException("Unexpected tickVisibilityMode: " + tickVisibilityMode);
1794+
}
17761795
}
17771796

17781797
/**
17791798
* Sets whether the tick marks are visible. Only used when the slider is in discrete mode.
17801799
*
17811800
* @param tickVisible The visibility of tick marks.
17821801
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1802+
* @deprecated Use {@link #setTickVisibilityMode(int)} instead.
17831803
*/
1804+
@Deprecated
17841805
public void setTickVisible(boolean tickVisible) {
1785-
if (this.tickVisible != tickVisible) {
1786-
this.tickVisible = tickVisible;
1806+
setTickVisibilityMode(convertToTickVisibilityMode(tickVisible));
1807+
}
1808+
1809+
@TickVisibilityMode
1810+
private int convertToTickVisibilityMode(boolean tickVisible) {
1811+
return tickVisible
1812+
? TICK_VISIBILITY_AUTO_LIMIT
1813+
: TICK_VISIBILITY_HIDDEN;
1814+
}
1815+
1816+
/**
1817+
* Returns the current tick visibility mode.
1818+
*
1819+
* @see #setTickVisibilityMode(int)
1820+
* @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1821+
*/
1822+
@TickVisibilityMode
1823+
public int getTickVisibilityMode() {
1824+
return tickVisibilityMode;
1825+
}
1826+
1827+
/**
1828+
* Sets the tick visibility mode. Only used when the slider is in discrete mode.
1829+
*
1830+
* @see #getTickVisibilityMode()
1831+
* @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1832+
*/
1833+
public void setTickVisibilityMode(@TickVisibilityMode int tickVisibilityMode) {
1834+
if (this.tickVisibilityMode != tickVisibilityMode) {
1835+
this.tickVisibilityMode = tickVisibilityMode;
17871836
postInvalidate();
17881837
}
17891838
}
@@ -2337,24 +2386,60 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
23372386
updateHaloHotspot();
23382387
}
23392388

2340-
private void maybeCalculateTicksCoordinates() {
2389+
private void updateTicksCoordinates() {
2390+
validateConfigurationIfDirty();
2391+
23412392
if (stepSize <= 0.0f) {
2393+
updateTicksCoordinates(/* tickCount= */ 0);
23422394
return;
23432395
}
23442396

2345-
validateConfigurationIfDirty();
2397+
final int tickCount;
2398+
switch (tickVisibilityMode) {
2399+
case TICK_VISIBILITY_VISIBLE_ALL:
2400+
tickCount = getDesiredTickCount();
2401+
break;
2402+
case TICK_VISIBILITY_AUTO_LIMIT:
2403+
tickCount = min(getDesiredTickCount(), getMaxTickCount());
2404+
break;
2405+
case TICK_VISIBILITY_AUTO_HIDE:
2406+
int desiredTickCount = getDesiredTickCount();
2407+
tickCount = desiredTickCount <= getMaxTickCount() ? desiredTickCount : 0;
2408+
break;
2409+
case TICK_VISIBILITY_HIDDEN:
2410+
tickCount = 0;
2411+
break;
2412+
default:
2413+
throw new RuntimeException("Unexpected tickVisibilityMode: " + tickVisibilityMode);
2414+
}
2415+
2416+
updateTicksCoordinates(tickCount);
2417+
}
2418+
2419+
private int getDesiredTickCount() {
2420+
return (int) ((valueTo - valueFrom) / stepSize + 1);
2421+
}
2422+
2423+
private int getMaxTickCount() {
2424+
return trackWidth / minTickSpacing + 1;
2425+
}
2426+
2427+
private void updateTicksCoordinates(int tickCount) {
2428+
if (tickCount == 0) {
2429+
ticksCoordinates = null;
2430+
return;
2431+
}
23462432

2347-
int tickCount = (int) ((valueTo - valueFrom) / stepSize + 1);
2348-
// Limit the tickCount if they will be too dense.
2349-
tickCount = min(tickCount, trackWidth / minTickSpacing + 1);
23502433
if (ticksCoordinates == null || ticksCoordinates.length != tickCount * 2) {
23512434
ticksCoordinates = new float[tickCount * 2];
23522435
}
23532436

23542437
float interval = trackWidth / (float) (tickCount - 1);
2438+
float trackCenterY = calculateTrackCenter();
2439+
23552440
for (int i = 0; i < tickCount * 2; i += 2) {
23562441
ticksCoordinates[i] = trackSidePadding + i / 2f * interval;
2357-
ticksCoordinates[i + 1] = calculateTrackCenter();
2442+
ticksCoordinates[i + 1] = trackCenterY;
23582443
}
23592444

23602445
if (isVertical()) {
@@ -2367,7 +2452,7 @@ private void updateTrackWidth(int width) {
23672452
trackWidth = max(width - trackSidePadding * 2, 0);
23682453

23692454
// Update the visible tick coordinates.
2370-
maybeCalculateTicksCoordinates();
2455+
updateTicksCoordinates();
23712456
}
23722457

23732458
private void updateHaloHotspot() {
@@ -2404,7 +2489,7 @@ protected void onDraw(@NonNull Canvas canvas) {
24042489
validateConfigurationIfDirty();
24052490

24062491
// Update the visible tick coordinates.
2407-
maybeCalculateTicksCoordinates();
2492+
updateTicksCoordinates();
24082493
}
24092494

24102495
super.onDraw(canvas);
@@ -2703,7 +2788,7 @@ private float[] getCornerRadii(float leftSide, float rightSide) {
27032788
}
27042789

27052790
private void maybeDrawTicks(@NonNull Canvas canvas) {
2706-
if (!tickVisible || stepSize <= 0.0f) {
2791+
if (ticksCoordinates == null || ticksCoordinates.length == 0) {
27072792
return;
27082793
}
27092794

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.slider;
18+
19+
import androidx.annotation.IntDef;
20+
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
24+
/**
25+
* Mode to specify the visibility of tick marks.
26+
*/
27+
@IntDef({
28+
TickVisibilityMode.TICK_VISIBILITY_VISIBLE_ALL,
29+
TickVisibilityMode.TICK_VISIBILITY_AUTO_LIMIT,
30+
TickVisibilityMode.TICK_VISIBILITY_AUTO_HIDE,
31+
TickVisibilityMode.TICK_VISIBILITY_HIDDEN
32+
})
33+
@Retention(RetentionPolicy.SOURCE)
34+
public @interface TickVisibilityMode {
35+
36+
/**
37+
* All tick marks will be drawn, even if they are spaced too densely.
38+
*/
39+
int TICK_VISIBILITY_VISIBLE_ALL = 0;
40+
41+
/**
42+
* All tick marks will be drawn if they are not spaced too densely. Otherwise, the maximum
43+
* allowed number of tick marks will be drawn.
44+
* Note that in this case, the drawn ticks may not match the actual snap values.
45+
*/
46+
int TICK_VISIBILITY_AUTO_LIMIT = 1;
47+
48+
/**
49+
* All tick marks will be drawn if they are not spaced too densely. Otherwise, the tick marks
50+
* will not be drawn.
51+
*/
52+
int TICK_VISIBILITY_AUTO_HIDE = 2;
53+
54+
/**
55+
* Tick marks will not be drawn.
56+
*/
57+
int TICK_VISIBILITY_HIDDEN = 3;
58+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<public name="tickRadiusActive" type="attr" />
3636
<public name="tickRadiusInactive" type="attr" />
3737
<public name="tickVisible" type="attr" />
38+
<public name="tickVisibilityMode" type="attr" />
3839
<public name="trackColorActive" type="attr" />
3940
<public name="trackColorInactive" type="attr" />
4041
<public name="trackHeight" type="attr" />

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,24 @@
7070
<attr name="tickRadiusActive" format="dimension" />
7171
<!-- The radius of the inactive tick. Only used when the slider is in discrete mode.-->
7272
<attr name="tickRadiusInactive" format="dimension" />
73-
<!-- Whether to show the tick marks. Only used when the slider is in discrete mode. -->
73+
<!-- Whether to show the tick marks. Only used when the slider is in discrete mode.
74+
{@deprecated Use tickVisibilityMode instead.} -->
7475
<attr name="tickVisible" format="boolean" />
76+
<!-- Mode to specify the visibility of tick marks. Only used when the slider is in
77+
discrete mode. -->
78+
<attr name="tickVisibilityMode" format="enum">
79+
<!-- All tick marks will be drawn, even if they are spaced too densely. -->
80+
<enum name="visibleAll" value="0" />
81+
<!-- All tick marks will be drawn if they are not spaced too densely. Otherwise,
82+
the maximum allowed number of tick marks will be drawn.
83+
Note that in this case, the drawn ticks may not match the actual snap values. -->
84+
<enum name="autoLimit" value="1" />
85+
<!-- All tick marks will be drawn if they are not spaced too densely. Otherwise,
86+
the tick marks will not be drawn. -->
87+
<enum name="autoHide" value="2" />
88+
<!-- Tick marks will not be drawn. -->
89+
<enum name="hidden" value="3" />
90+
</attr>
7591
<!-- The color of the track. -->
7692
<attr name="trackColor" />
7793
<!-- The color of active portion of the track. -->

lib/javatests/com/google/android/material/slider/SliderConfigTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,20 @@ public static Iterable<Object[]> data() {
8989
new Object[] {
9090
/* valueFrom = */ 10f,
9191
/* valueTo = */ 3f,
92-
/* stepValue = */ 2f,
92+
/* stepValue = */ 0f,
9393
/* value = */ 10f,
9494
IllegalStateException.class,
9595
},
96-
new Object[] {
97-
/* valueFrom = */ 0f, /* valueTo = */ 1f, /* stepValue = */ 0, /* value = */ 0f, null,
98-
},
9996
new Object[] {
10097
/* valueFrom = */ 10f,
10198
/* valueTo = */ 3f,
10299
/* stepValue = */ 2f,
103100
/* value = */ 10f,
104101
IllegalStateException.class,
105102
},
103+
new Object[] {
104+
/* valueFrom = */ 0f, /* valueTo = */ 1f, /* stepValue = */ 0, /* value = */ 0f, null,
105+
},
106106
new Object[] {
107107
/* valueFrom = */ 0f,
108108
/* valueTo = */ 5f,

0 commit comments

Comments
 (0)