Skip to content

Commit dbfcd35

Browse files
veganafrodrchen
authored andcommitted
[Catalog][Slider] continuous slider demo should use a single switch
PiperOrigin-RevId: 439382639
1 parent f915ffd commit dbfcd35

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

catalog/java/io/material/catalog/slider/SliderContinuousDemoFragment.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
import io.material.catalog.R;
2020

2121
import android.os.Bundle;
22-
import androidx.appcompat.widget.SwitchCompat;
2322
import android.view.LayoutInflater;
2423
import android.view.View;
2524
import android.view.ViewGroup;
2625
import android.widget.TextView;
27-
import androidx.annotation.IdRes;
2826
import androidx.annotation.Nullable;
2927
import com.google.android.material.slider.Slider;
28+
import com.google.android.material.switchmaterial.SwitchMaterial;
3029
import io.material.catalog.feature.DemoFragment;
3130

3231
/**
@@ -40,27 +39,28 @@ public View onCreateDemoView(
4039
LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
4140
View view = inflater.inflate(R.layout.cat_slider_demo_continuous, viewGroup, false);
4241

43-
setUpSlider(view, R.id.switch_button_1, R.id.slider_1, R.id.value_1, "%.0f");
44-
setUpSlider(view, R.id.switch_button_2, R.id.slider_2, R.id.value_2, "%.0f");
45-
setUpSlider(view, R.id.switch_button_3, R.id.slider_3, R.id.value_3, "%.2f");
42+
Slider sliderOne = view.findViewById(R.id.slider_1);
43+
Slider sliderTwo = view.findViewById(R.id.slider_2);
44+
Slider sliderThree = view.findViewById(R.id.slider_3);
45+
46+
SwitchMaterial switchButton = view.findViewById(R.id.switch_button);
47+
48+
switchButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
49+
sliderOne.setEnabled(isChecked);
50+
sliderTwo.setEnabled(isChecked);
51+
sliderThree.setEnabled(isChecked);
52+
});
53+
54+
setUpSlider(sliderOne, view.findViewById(R.id.value_1), "%.0f");
55+
setUpSlider(sliderTwo, view.findViewById(R.id.value_2), "%.0f");
56+
setUpSlider(sliderThree, view.findViewById(R.id.value_3), "%.2f");
4657

4758
return view;
4859
}
4960

50-
private void setUpSlider(
51-
View view,
52-
@IdRes int switchId,
53-
@IdRes int sliderId,
54-
@IdRes int valueId,
55-
final String valueFormat) {
56-
final TextView sliderValue = view.findViewById(valueId);
57-
final Slider slider = view.findViewById(sliderId);
58-
slider.addOnChangeListener(
59-
(slider1, value, fromUser) -> sliderValue.setText(String.format(valueFormat, value)));
61+
private void setUpSlider(Slider slider, TextView textView, final String valueFormat) {
62+
slider.addOnChangeListener((slider1, value, fromUser) -> textView
63+
.setText(String.format(valueFormat, value)));
6064
slider.setValue(slider.getValueFrom());
61-
SwitchCompat switchButton = view.findViewById(switchId);
62-
switchButton.setOnCheckedChangeListener(
63-
(buttonView, isChecked) -> slider.setEnabled(isChecked));
64-
switchButton.setChecked(true);
6565
}
6666
}

catalog/java/io/material/catalog/slider/SliderDiscreteDemoFragment.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import io.material.catalog.R;
2020

2121
import android.os.Bundle;
22-
import androidx.appcompat.widget.SwitchCompat;
2322
import android.view.LayoutInflater;
2423
import android.view.View;
2524
import android.view.ViewGroup;
2625
import androidx.annotation.Nullable;
2726
import com.google.android.material.slider.BasicLabelFormatter;
2827
import com.google.android.material.slider.Slider;
28+
import com.google.android.material.switchmaterial.SwitchMaterial;
2929
import io.material.catalog.feature.DemoFragment;
3030
import java.util.Arrays;
3131
import java.util.List;
@@ -53,8 +53,7 @@ public View onCreateDemoView(
5353
view.findViewById(R.id.slider_5),
5454
view.findViewById(R.id.slider_6)
5555
);
56-
sliders.get(3).setLabelFormatter(new BasicLabelFormatter());
57-
SwitchCompat switchButton = view.findViewById(R.id.switch_button);
56+
SwitchMaterial switchButton = view.findViewById(R.id.switch_button);
5857

5958
switchButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
6059
for (Slider slider : sliders) {

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737
android:layout_height="wrap_content"
3838
android:gravity="center_vertical">
3939

40-
<com.google.android.material.switchmaterial.SwitchMaterial
41-
android:id="@+id/switch_button_1"
42-
android:layout_width="wrap_content"
43-
android:layout_height="wrap_content" />
44-
4540
<com.google.android.material.slider.Slider
4641
android:id="@+id/slider_1"
4742
android:layout_width="0dp"
@@ -69,11 +64,6 @@
6964
android:layout_height="wrap_content"
7065
android:gravity="center_vertical">
7166

72-
<com.google.android.material.switchmaterial.SwitchMaterial
73-
android:id="@+id/switch_button_2"
74-
android:layout_width="wrap_content"
75-
android:layout_height="wrap_content" />
76-
7767
<com.google.android.material.slider.Slider
7868
android:id="@+id/slider_2"
7969
android:layout_width="0dp"
@@ -101,11 +91,6 @@
10191
android:layout_height="wrap_content"
10292
android:gravity="center_vertical">
10393

104-
<com.google.android.material.switchmaterial.SwitchMaterial
105-
android:id="@+id/switch_button_3"
106-
android:layout_width="wrap_content"
107-
android:layout_height="wrap_content" />
108-
10994
<com.google.android.material.slider.Slider
11095
android:id="@+id/slider_3"
11196
android:layout_width="0dp"
@@ -121,5 +106,13 @@
121106
android:layout_width="@dimen/cat_slider_value_width"
122107
android:layout_height="wrap_content" />
123108
</LinearLayout>
109+
110+
<com.google.android.material.switchmaterial.SwitchMaterial
111+
android:id="@+id/switch_button"
112+
android:layout_width="wrap_content"
113+
android:layout_height="wrap_content"
114+
android:layout_gravity="center_horizontal"
115+
android:checked="true"
116+
android:text="@string/cat_slider_enabled"/>
124117
</LinearLayout>
125118
</ScrollView>

0 commit comments

Comments
 (0)