Skip to content

Commit 8e52e94

Browse files
committed
[ButtonGroup] Added a click action and toggleable control in button group demo.
PiperOrigin-RevId: 750767384
1 parent 0bd1749 commit 8e52e94

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

catalog/java/io/material/catalog/button/ButtonGroupDemoFragment.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.android.material.button.MaterialButton;
3131
import com.google.android.material.button.MaterialButtonGroup;
3232
import com.google.android.material.materialswitch.MaterialSwitch;
33+
import com.google.android.material.snackbar.Snackbar;
3334
import io.material.catalog.feature.DemoFragment;
3435
import io.material.catalog.feature.DemoUtils;
3536
import java.util.List;
@@ -66,7 +67,14 @@ public View onCreateDemoView(
6667
buttonGroup.requestLayout();
6768
}
6869
});
69-
70+
MaterialSwitch groupToggleableToggle = view.findViewById(R.id.switch_toggle);
71+
groupToggleableToggle.setOnCheckedChangeListener(
72+
(buttonView, isChecked) -> {
73+
for (MaterialButton button : buttons) {
74+
button.setCheckable(isChecked);
75+
button.refreshDrawableState();
76+
}
77+
});
7078
MaterialSwitch groupEnabledToggle = view.findViewById(R.id.switch_enable);
7179
groupEnabledToggle.setOnCheckedChangeListener(
7280
(buttonView, isChecked) -> {
@@ -82,6 +90,18 @@ public View onCreateDemoView(
8290
button.setOpticalCenterEnabled(isChecked);
8391
}
8492
});
93+
for (MaterialButton button : buttons) {
94+
MaterialButtonGroup buttonGroup = (MaterialButtonGroup) button.getParent();
95+
button.setOnClickListener(
96+
v -> {
97+
String snackbarText = button.getText() + "";
98+
if (snackbarText.isEmpty()) {
99+
snackbarText = button.getContentDescription() + "";
100+
}
101+
snackbarText += " button clicked.";
102+
Snackbar.make(buttonGroup, snackbarText, Snackbar.LENGTH_LONG).show();
103+
});
104+
}
85105
return view;
86106
}
87107

catalog/java/io/material/catalog/button/res/layout/cat_buttons_group_fragment.xml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,35 @@
3737
android:id="@+id/orientation_switch_toggle"
3838
android:paddingTop="16dp"
3939
android:layout_width="wrap_content"
40-
android:layout_height="match_parent"
40+
android:layout_height="wrap_content"
4141
android:layout_gravity="center"
4242
android:checked="false"
4343
android:enabled="true"
4444
android:text="@string/cat_button_vertical_orientation" />
45+
<com.google.android.material.materialswitch.MaterialSwitch
46+
android:id="@+id/switch_toggle"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:paddingTop="16dp"
50+
android:layout_gravity="center"
51+
android:checked="false"
52+
android:enabled="true"
53+
android:text="@string/cat_button_toggleable"/>
4554
<com.google.android.material.materialswitch.MaterialSwitch
4655
android:id="@+id/switch_enable"
4756
android:layout_width="wrap_content"
48-
android:layout_height="match_parent"
57+
android:layout_height="wrap_content"
4958
android:paddingTop="16dp"
50-
android:paddingBottom="16dp"
5159
android:layout_gravity="center"
5260
android:checked="true"
5361
android:enabled="true"
5462
android:text="@string/cat_button_enable" />
5563
<com.google.android.material.materialswitch.MaterialSwitch
5664
android:id="@+id/switch_optical_center"
5765
android:paddingTop="16dp"
66+
android:paddingBottom="16dp"
5867
android:layout_width="wrap_content"
59-
android:layout_height="match_parent"
68+
android:layout_height="wrap_content"
6069
android:layout_gravity="center"
6170
android:checked="false"
6271
android:enabled="true"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,5 @@
136136
<string name="cat_button_previous_icon">previous icon</string>
137137
<string name="cat_button_play_icon">play icon</string>
138138
<string name="cat_button_next_icon">next icon</string>
139+
<string name="cat_button_toggleable">Toggleable</string>
139140
</resources>

0 commit comments

Comments
 (0)