Skip to content

Commit ca8594d

Browse files
pekingmeveganafro
authored andcommitted
[Slider] Added LABEL_VISIBLE to label behavior and added a demo fragment in catalog.
Resolves #1316 PiperOrigin-RevId: 433570701
1 parent e7a691e commit ca8594d

File tree

8 files changed

+296
-53
lines changed

8 files changed

+296
-53
lines changed

catalog/java/io/material/catalog/slider/SliderFragment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public Fragment createFragment() {
7777
return new SliderScrollContainerDemoFragment();
7878
}
7979
});
80+
additionalDemos.add(
81+
new Demo(R.string.cat_slider_demo_label_behavior_title) {
82+
@Override
83+
public Fragment createFragment() {
84+
return new SliderLabelBehaviorDemoFragment();
85+
}
86+
});
8087
return additionalDemos;
8188
}
8289

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019 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 io.material.catalog.slider;
18+
19+
import io.material.catalog.R;
20+
21+
import android.os.Bundle;
22+
import androidx.appcompat.widget.SwitchCompat;
23+
import android.view.LayoutInflater;
24+
import android.view.View;
25+
import android.view.ViewGroup;
26+
import androidx.annotation.IdRes;
27+
import androidx.annotation.NonNull;
28+
import androidx.annotation.Nullable;
29+
import com.google.android.material.slider.RangeSlider;
30+
import com.google.android.material.slider.Slider;
31+
import io.material.catalog.feature.DemoFragment;
32+
33+
/**
34+
* Fragment to display a few basic uses of the {@link Slider} widget with different label behaviors
35+
* for the Catalog app.
36+
*/
37+
public class SliderLabelBehaviorDemoFragment extends DemoFragment {
38+
39+
@Nullable
40+
@Override
41+
public View onCreateDemoView(
42+
@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
43+
View view =
44+
inflater.inflate(
45+
R.layout.cat_slider_demo_label_behavior, viewGroup, false /* attachToRoot */);
46+
47+
setUpSlider(view, R.id.switch_button_1, R.id.slider_1);
48+
setUpSlider(view, R.id.switch_button_2, R.id.slider_2);
49+
setUpSlider(view, R.id.switch_button_3, R.id.slider_3);
50+
setUpSlider(view, R.id.switch_button_4, R.id.slider_4);
51+
52+
return view;
53+
}
54+
55+
private void setUpSlider(
56+
View view, @IdRes int switchId, @IdRes int sliderId) {
57+
final RangeSlider slider = view.findViewById(sliderId);
58+
SwitchCompat switchButton = view.findViewById(switchId);
59+
switchButton.setOnCheckedChangeListener(
60+
(buttonView, isChecked) -> slider.setEnabled(isChecked));
61+
switchButton.setChecked(true);
62+
}
63+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2019 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<ScrollView
18+
xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent">
22+
23+
<LinearLayout
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:paddingLeft="16dp"
27+
android:paddingRight="16dp"
28+
android:orientation="vertical">
29+
30+
<TextView
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:paddingTop="@dimen/cat_slider_title_top_padding"
34+
android:text="@string/cat_slider_label_floating"/>
35+
36+
<LinearLayout
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:gravity="center_vertical">
40+
41+
<androidx.appcompat.widget.SwitchCompat
42+
android:id="@+id/switch_button_1"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content" />
45+
46+
<com.google.android.material.slider.RangeSlider
47+
android:id="@+id/slider_1"
48+
android:layout_width="0dp"
49+
android:layout_height="wrap_content"
50+
android:layout_weight="1"
51+
android:layout_marginLeft="@dimen/cat_slider_left_margin"
52+
android:layout_marginStart="@dimen/cat_slider_left_margin"
53+
android:valueFrom="0"
54+
android:valueTo="10"
55+
app:values="@array/initial_slider_values"
56+
app:labelBehavior="floating"/>
57+
</LinearLayout>
58+
59+
<TextView
60+
android:layout_width="wrap_content"
61+
android:layout_height="wrap_content"
62+
android:paddingTop="@dimen/cat_slider_title_top_padding"
63+
android:text="@string/cat_slider_label_within_bounds"/>
64+
65+
<LinearLayout
66+
android:layout_width="match_parent"
67+
android:layout_height="wrap_content"
68+
android:gravity="center_vertical">
69+
70+
<androidx.appcompat.widget.SwitchCompat
71+
android:id="@+id/switch_button_2"
72+
android:layout_width="wrap_content"
73+
android:layout_height="wrap_content" />
74+
75+
<com.google.android.material.slider.RangeSlider
76+
android:id="@+id/slider_2"
77+
android:layout_width="0dp"
78+
android:layout_height="wrap_content"
79+
android:layout_weight="1"
80+
android:layout_marginLeft="@dimen/cat_slider_left_margin"
81+
android:layout_marginStart="@dimen/cat_slider_left_margin"
82+
android:valueFrom="0"
83+
android:valueTo="10"
84+
app:values="@array/initial_slider_values"
85+
app:labelBehavior="withinBounds"/>
86+
</LinearLayout>
87+
88+
<TextView
89+
android:layout_width="wrap_content"
90+
android:layout_height="wrap_content"
91+
android:paddingTop="@dimen/cat_slider_title_top_padding"
92+
android:text="@string/cat_slider_label_gone"/>
93+
94+
<LinearLayout
95+
android:layout_width="match_parent"
96+
android:layout_height="wrap_content"
97+
android:gravity="center_vertical">
98+
99+
<androidx.appcompat.widget.SwitchCompat
100+
android:id="@+id/switch_button_3"
101+
android:layout_width="wrap_content"
102+
android:layout_height="wrap_content" />
103+
104+
<com.google.android.material.slider.RangeSlider
105+
android:id="@+id/slider_3"
106+
android:layout_width="0dp"
107+
android:layout_height="wrap_content"
108+
android:layout_weight="1"
109+
android:layout_marginLeft="@dimen/cat_slider_left_margin"
110+
android:layout_marginStart="@dimen/cat_slider_left_margin"
111+
android:valueFrom="0"
112+
android:valueTo="10"
113+
app:values="@array/initial_slider_values"
114+
app:labelBehavior="gone"/>
115+
</LinearLayout>
116+
117+
<TextView
118+
android:layout_width="wrap_content"
119+
android:layout_height="wrap_content"
120+
android:paddingTop="@dimen/cat_slider_title_top_padding"
121+
android:text="@string/cat_slider_label_visible"/>
122+
123+
<LinearLayout
124+
android:layout_width="match_parent"
125+
android:layout_height="wrap_content"
126+
android:gravity="center_vertical">
127+
128+
<androidx.appcompat.widget.SwitchCompat
129+
android:id="@+id/switch_button_4"
130+
android:layout_width="wrap_content"
131+
android:layout_height="wrap_content" />
132+
133+
<com.google.android.material.slider.RangeSlider
134+
android:id="@+id/slider_4"
135+
android:layout_width="0dp"
136+
android:layout_height="wrap_content"
137+
android:layout_weight="1"
138+
android:layout_marginLeft="@dimen/cat_slider_left_margin"
139+
android:layout_marginStart="@dimen/cat_slider_left_margin"
140+
android:valueFrom="0"
141+
android:valueTo="10"
142+
app:values="@array/initial_slider_values"
143+
app:labelBehavior="visible"/>
144+
</LinearLayout>
145+
</LinearLayout>
146+
</ScrollView>

catalog/java/io/material/catalog/slider/res/values/strings.xml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
limitations under the License.
1616
-->
1717
<resources>
18-
<string name="cat_slider_title" description="Title for the screen that showcases demonstrative usages of the Slider widget">Slider</string>
19-
<string name="cat_slider_demo_continuous_title" description="Title for the continuous Slider widget demo">Continuous Slider demo</string>
20-
<string name="cat_slider_demo_discrete_title" description="Title for the discrete Slider widget demo">Discrete Slider demo</string>
21-
<string name="cat_slider_demo_scroll_container_title" description="Title for the Slider inside scrolling container demo">Slider in scrolling container demo</string>
18+
<string name="cat_slider_title" description="Title for the screen that showcases demonstrative usages of the Slider widget [CHAR LIMIT=NONE]">Slider</string>
19+
<string name="cat_slider_demo_continuous_title" description="Title for the continuous Slider widget demo [CHAR LIMIT=NONE]">Continuous Slider demo</string>
20+
<string name="cat_slider_demo_discrete_title" description="Title for the discrete Slider widget demo [CHAR LIMIT=NONE]">Discrete Slider demo</string>
21+
<string name="cat_slider_demo_scroll_container_title" description="Title for the Slider inside scrolling container demo [CHAR LIMIT=NONE]">Slider in scrolling container demo</string>
22+
<string name="cat_slider_demo_label_behavior_title" description="Tile for the Slider with different label behaviors demo [CHAR LIMIT=NONE]">Slider label behavior demo</string>
2223

2324
<string name="cat_slider_description" description="Body text describing the Slider widget within the design system [CHAR LIMIT=NONE]">
2425
Sliders let users select from a range of values by moving the slider thumb.
@@ -28,14 +29,18 @@
2829
Discrete sliders allow users to select a specific value from a range.
2930
</string>
3031

31-
<string name="cat_slider_set" description="The label for a set button">Set</string>
32-
<string name="cat_slider_title_1" description="The title for the first demonstration slider">This one goes to eleven</string>
33-
<string name="cat_slider_title_2" description="The title for a demonstration slider">From 100 to 1000</string>
34-
<string name="cat_slider_title_3" description="The title for a demonstration slider">Negative numbers!</string>
35-
<string name="cat_slider_title_4" description="The title for a demonstration slider">With a label formatter</string>
36-
<string name="cat_slider_title_5" description="The title for a demonstration slider">I can have decimal numbers?</string>
37-
<string name="cat_slider_title_6" description="The title for a demonstration slider">Without tick marks</string>
38-
<string name="cat_slider_start_touch_description" description="Indicates the slider is now being touched">Slider started being touched</string>
39-
<string name="cat_slider_stop_touch_description" description="Indicates the slider stopped being touched">Slider stopped being touched</string>
32+
<string name="cat_slider_set" description="The label for a set button [CHAR LIMIT=NONE]">Set</string>
33+
<string name="cat_slider_title_1" description="The title for the first demonstration slider [CHAR LIMIT=NONE]">This one goes to eleven</string>
34+
<string name="cat_slider_title_2" description="The title for a demonstration slider [CHAR LIMIT=NONE]">From 100 to 1000</string>
35+
<string name="cat_slider_title_3" description="The title for a demonstration slider [CHAR LIMIT=NONE]">Negative numbers!</string>
36+
<string name="cat_slider_title_4" description="The title for a demonstration slider [CHAR LIMIT=NONE]">With a label formatter</string>
37+
<string name="cat_slider_title_5" description="The title for a demonstration slider [CHAR LIMIT=NONE]">I can have decimal numbers?</string>
38+
<string name="cat_slider_title_6" description="The title for a demonstration slider [CHAR LIMIT=NONE]">Without tick marks</string>
39+
<string name="cat_slider_start_touch_description" description="Indicates the slider is now being touched [CHAR LIMIT=NONE]">Slider started being touched</string>
40+
<string name="cat_slider_stop_touch_description" description="Indicates the slider stopped being touched [CHAR LIMIT=NONE]">Slider stopped being touched</string>
41+
<string name="cat_slider_label_floating" description="The title for the demonstration slider with floating labels [CHAR LIMIT=NONE]">This one has floating labels</string>
42+
<string name="cat_slider_label_within_bounds" description="The title for the demonstration slider with labels within bounds [CHAR LIMIT=NONE]">This one has labels within bounds</string>
43+
<string name="cat_slider_label_gone" description="The title for the demonstration slider with gone labels [CHAR LIMIT=NONE]">This one shows no labels</string>
44+
<string name="cat_slider_label_visible" description="The title for the first demonstration slider [CHAR LIMIT=NONE]">This one always shows labels</string>
4045

4146
</resources>

docs/components/Slider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ The modes of `app:labelBehavior` are:
145145
view
146146
* `withinBounds` - draws the label floating within the bounds of this view
147147
* `gone` - prevents the label from being drawn
148+
* `visible` - always draws the label
148149

149150
### Setting a `LabelFormatter`
150151

0 commit comments

Comments
 (0)