Skip to content

Commit 82e1130

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

File tree

7 files changed

+175
-25
lines changed

7 files changed

+175
-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: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
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;
3235
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
3336
import static java.lang.Float.compare;
3437
import static java.lang.Math.abs;
@@ -157,8 +160,10 @@
157160
* discrete mode. This is a short hand for setting both the {@code tickColorActive} and {@code
158161
* tickColorInactive} to the same thing. This takes precedence over {@code tickColorActive}
159162
* and {@code tickColorInactive}.
160-
* <li>{@code tickVisible}: Whether to show the tick marks. Only used when the slider is in
161-
* discrete mode.
163+
* <li>{@code tickVisible} (<b>deprecated</b>, use {@code tickVisibilityMode} instead):
164+
* Whether to show the tick marks. Only used when the slider is in discrete mode.
165+
* <li>{@code tickVisibilityMode}: Mode to specify the visibility of tick marks. Only used when
166+
* the slider is in discrete mode.
162167
* <li>{@code trackColorActive}: The color of the active part of the track.
163168
* <li>{@code trackColorInactive}: The color of the inactive part of the track.
164169
* <li>{@code trackColor}: The color of the whole track. This is a short hand for setting both the
@@ -210,6 +215,7 @@
210215
* @attr ref com.google.android.material.R.styleable#Slider_tickColorActive
211216
* @attr ref com.google.android.material.R.styleable#Slider_tickColorInactive
212217
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
218+
* @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
213219
* @attr ref com.google.android.material.R.styleable#Slider_trackColor
214220
* @attr ref com.google.android.material.R.styleable#Slider_trackColorActive
215221
* @attr ref com.google.android.material.R.styleable#Slider_trackColorInactive
@@ -352,7 +358,7 @@ abstract class BaseSlider<
352358
private int focusedThumbIdx = -1;
353359
private float stepSize = 0.0f;
354360
private float[] ticksCoordinates;
355-
private boolean tickVisible = true;
361+
private int tickVisibilityMode;
356362
private int tickActiveRadius;
357363
private int tickInactiveRadius;
358364
private int trackWidth;
@@ -566,7 +572,10 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
566572
? haloColor
567573
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
568574

569-
tickVisible = a.getBoolean(R.styleable.Slider_tickVisible, true);
575+
tickVisibilityMode = a.hasValue(R.styleable.Slider_tickVisibilityMode)
576+
? a.getInt(R.styleable.Slider_tickVisibilityMode, -1)
577+
: convertToTickVisibilityMode(a.getBoolean(R.styleable.Slider_tickVisible, true));
578+
570579
boolean hasTickColor = a.hasValue(R.styleable.Slider_tickColor);
571580
int tickColorInactiveRes =
572581
hasTickColor ? R.styleable.Slider_tickColor : R.styleable.Slider_tickColorInactive;
@@ -1768,22 +1777,60 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
17681777
/**
17691778
* Returns whether the tick marks are visible. Only used when the slider is in discrete mode.
17701779
*
1771-
* @see #setTickVisible(boolean)
17721780
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
17731781
*/
17741782
public boolean isTickVisible() {
1775-
return tickVisible;
1783+
switch (tickVisibilityMode) {
1784+
case TICK_VISIBILITY_AUTO_LIMIT:
1785+
return true;
1786+
case TICK_VISIBILITY_AUTO_HIDE:
1787+
return getDesiredTickCount() <= getMaxTickCount();
1788+
case TICK_VISIBILITY_HIDDEN:
1789+
return false;
1790+
default:
1791+
throw new RuntimeException("Unexpected tickVisibilityMode: " + tickVisibilityMode);
1792+
}
17761793
}
17771794

17781795
/**
17791796
* Sets whether the tick marks are visible. Only used when the slider is in discrete mode.
17801797
*
17811798
* @param tickVisible The visibility of tick marks.
17821799
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1800+
* @deprecated Use {@link #setTickVisibilityMode(int)} instead.
17831801
*/
1802+
@Deprecated
17841803
public void setTickVisible(boolean tickVisible) {
1785-
if (this.tickVisible != tickVisible) {
1786-
this.tickVisible = tickVisible;
1804+
setTickVisibilityMode(convertToTickVisibilityMode(tickVisible));
1805+
}
1806+
1807+
@TickVisibilityMode
1808+
private int convertToTickVisibilityMode(boolean tickVisible) {
1809+
return tickVisible
1810+
? TICK_VISIBILITY_AUTO_LIMIT
1811+
: TICK_VISIBILITY_HIDDEN;
1812+
}
1813+
1814+
/**
1815+
* Returns the current tick visibility mode.
1816+
*
1817+
* @see #setTickVisibilityMode(int)
1818+
* @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1819+
*/
1820+
@TickVisibilityMode
1821+
public int getTickVisibilityMode() {
1822+
return tickVisibilityMode;
1823+
}
1824+
1825+
/**
1826+
* Sets the tick visibility mode. Only used when the slider is in discrete mode.
1827+
*
1828+
* @see #getTickVisibilityMode()
1829+
* @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1830+
*/
1831+
public void setTickVisibilityMode(@TickVisibilityMode int tickVisibilityMode) {
1832+
if (this.tickVisibilityMode != tickVisibilityMode) {
1833+
this.tickVisibilityMode = tickVisibilityMode;
17871834
postInvalidate();
17881835
}
17891836
}
@@ -2337,24 +2384,57 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
23372384
updateHaloHotspot();
23382385
}
23392386

2340-
private void maybeCalculateTicksCoordinates() {
2387+
private void updateTicksCoordinates() {
2388+
validateConfigurationIfDirty();
2389+
23412390
if (stepSize <= 0.0f) {
2391+
updateTicksCoordinates(/* tickCount= */ 0);
23422392
return;
23432393
}
23442394

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

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);
23502428
if (ticksCoordinates == null || ticksCoordinates.length != tickCount * 2) {
23512429
ticksCoordinates = new float[tickCount * 2];
23522430
}
23532431

23542432
float interval = trackWidth / (float) (tickCount - 1);
2433+
float trackCenterY = calculateTrackCenter();
2434+
23552435
for (int i = 0; i < tickCount * 2; i += 2) {
23562436
ticksCoordinates[i] = trackSidePadding + i / 2f * interval;
2357-
ticksCoordinates[i + 1] = calculateTrackCenter();
2437+
ticksCoordinates[i + 1] = trackCenterY;
23582438
}
23592439

23602440
if (isVertical()) {
@@ -2367,7 +2447,7 @@ private void updateTrackWidth(int width) {
23672447
trackWidth = max(width - trackSidePadding * 2, 0);
23682448

23692449
// Update the visible tick coordinates.
2370-
maybeCalculateTicksCoordinates();
2450+
updateTicksCoordinates();
23712451
}
23722452

23732453
private void updateHaloHotspot() {
@@ -2404,7 +2484,7 @@ protected void onDraw(@NonNull Canvas canvas) {
24042484
validateConfigurationIfDirty();
24052485

24062486
// Update the visible tick coordinates.
2407-
maybeCalculateTicksCoordinates();
2487+
updateTicksCoordinates();
24082488
}
24092489

24102490
super.onDraw(canvas);
@@ -2703,7 +2783,7 @@ private float[] getCornerRadii(float leftSide, float rightSide) {
27032783
}
27042784

27052785
private void maybeDrawTicks(@NonNull Canvas canvas) {
2706-
if (!tickVisible || stepSize <= 0.0f) {
2786+
if (ticksCoordinates == null || ticksCoordinates.length == 0) {
27072787
return;
27082788
}
27092789

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_AUTO_LIMIT,
29+
TickVisibilityMode.TICK_VISIBILITY_AUTO_HIDE,
30+
TickVisibilityMode.TICK_VISIBILITY_HIDDEN
31+
})
32+
@Retention(RetentionPolicy.SOURCE)
33+
public @interface TickVisibilityMode {
34+
35+
/**
36+
* All tick marks will be drawn if they are not spaced too densely. Otherwise, the maximum
37+
* allowed number of tick marks will be drawn.
38+
* Note that in this case the drawn ticks may not match the actual snap values.
39+
*/
40+
int TICK_VISIBILITY_AUTO_LIMIT = 0;
41+
42+
/**
43+
* All tick marks will be drawn if they are not spaced too densely. Otherwise, the tick marks
44+
* will not be drawn.
45+
*/
46+
int TICK_VISIBILITY_AUTO_HIDE = 1;
47+
48+
/**
49+
* Tick marks will not be drawn.
50+
*/
51+
int TICK_VISIBILITY_HIDDEN = 2;
52+
}

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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,22 @@
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 if they are not spaced too densely. Otherwise,
80+
the maximum allowed number of tick marks will be drawn.
81+
Note that in this case the drawn ticks may not match the actual snap values. -->
82+
<enum name="autoLimit" value="0" />
83+
<!-- All tick marks will be drawn if they are not spaced too densely. Otherwise,
84+
the tick marks will not be drawn. -->
85+
<enum name="autoHide" value="1" />
86+
<!-- Tick marks will not be drawn. -->
87+
<enum name="hidden" value="2" />
88+
</attr>
7589
<!-- The color of the track. -->
7690
<attr name="trackColor" />
7791
<!-- 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)