Skip to content

Commit 4f55422

Browse files
committed
[ButtonGroup] Add a demo to programmatically add/remove buttons in a button group.
PiperOrigin-RevId: 751060643
1 parent 8e52e94 commit 4f55422

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2025 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+
package io.material.catalog.button;
17+
18+
import io.material.catalog.R;
19+
20+
import android.annotation.SuppressLint;
21+
import android.os.Bundle;
22+
import android.view.LayoutInflater;
23+
import android.view.View;
24+
import android.view.View.OnClickListener;
25+
import android.view.ViewGroup;
26+
import android.widget.Button;
27+
import android.widget.LinearLayout.LayoutParams;
28+
import androidx.annotation.NonNull;
29+
import androidx.annotation.Nullable;
30+
import com.google.android.material.button.MaterialButton;
31+
import com.google.android.material.button.MaterialButtonGroup;
32+
import io.material.catalog.feature.DemoFragment;
33+
34+
/** A fragment that displays a button group demo for adding and removing buttons at runtime. */
35+
public class ButtonGroupRuntimeDemoFragment extends DemoFragment {
36+
37+
private static final int MAX_COUNT = 10;
38+
private int buttonCount;
39+
private Button addButton;
40+
private Button removeButton;
41+
42+
/**
43+
* Create a Demo View with {@link MaterialButtonGroup}, in which, buttons are added and removed at
44+
* runtime.
45+
*/
46+
@Nullable
47+
@Override
48+
public View onCreateDemoView(
49+
@NonNull LayoutInflater layoutInflater,
50+
@Nullable ViewGroup viewGroup,
51+
@Nullable Bundle bundle) {
52+
View view =
53+
layoutInflater.inflate(
54+
R.layout.cat_buttons_group_runtime_fragment, viewGroup, /* attachToRoot= */ false);
55+
56+
MaterialButtonGroup buttonGroup = view.findViewById(R.id.cat_dynamic_button_group);
57+
addButton = view.findViewById(R.id.cat_add_button);
58+
removeButton = view.findViewById(R.id.cat_remove_button);
59+
updateControl();
60+
addButton.setOnClickListener(
61+
new OnClickListener() {
62+
@SuppressLint("SetTextI18n")
63+
@Override
64+
public void onClick(View v) {
65+
MaterialButton button = new MaterialButton(view.getContext());
66+
button.setText("Button");
67+
buttonGroup.addView(
68+
button,
69+
-1,
70+
new LayoutParams(
71+
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
72+
buttonCount++;
73+
updateControl();
74+
}
75+
});
76+
removeButton.setOnClickListener(
77+
v -> {
78+
buttonGroup.removeViewAt(buttonGroup.getChildCount() - 1);
79+
buttonCount--;
80+
updateControl();
81+
});
82+
83+
return view;
84+
}
85+
86+
private void updateControl(){
87+
if(buttonCount == 0){
88+
removeButton.setEnabled(false);
89+
}else if(buttonCount == MAX_COUNT){
90+
addButton.setEnabled(false);
91+
}else{
92+
addButton.setEnabled(true);
93+
removeButton.setEnabled(true);
94+
}
95+
}
96+
}

catalog/java/io/material/catalog/button/ButtonsFragment.java

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public List<Demo> getAdditionalDemos() {
6767
public Fragment createFragment() {
6868
return new ButtonGroupDistributionDemoFragment();
6969
}
70+
},
71+
new Demo(R.string.cat_buttons_group_runtime) {
72+
@Nullable
73+
@Override
74+
public Fragment createFragment() {
75+
return new ButtonGroupRuntimeDemoFragment();
76+
}
7077
});
7178
}
7279

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 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+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:paddingTop="8dp"
22+
android:paddingHorizontal="8dp"
23+
android:gravity="center_horizontal"
24+
android:orientation="vertical">
25+
<com.google.android.material.button.MaterialButtonGroup
26+
android:id="@+id/cat_dynamic_button_group"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content">
29+
</com.google.android.material.button.MaterialButtonGroup>
30+
<Button
31+
android:id="@+id/cat_add_button"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:text="@string/cat_button_group_runtime_add"/>
35+
<Button
36+
android:id="@+id/cat_remove_button"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:text="@string/cat_button_group_runtime_remove"/>
40+
</LinearLayout>

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

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<string name="cat_toc_buttons">Buttons</string>
2121
<string name="cat_buttons_group" translatable="false">Buttons Group</string>
2222
<string name="cat_buttons_group_distribution" translatable="false">Buttons Group Distribution</string>
23+
<string name="cat_buttons_group_runtime" translatable="false">Buttons Group Runtime</string>
2324
<string name="cat_buttons_toggle_group">Buttons Toggle Group</string>
2425
<string name="cat_split_button" translatable="false">Split Button</string>
2526
<string name="cat_buttons_title">Buttons</string>
@@ -137,4 +138,6 @@
137138
<string name="cat_button_play_icon">play icon</string>
138139
<string name="cat_button_next_icon">next icon</string>
139140
<string name="cat_button_toggleable">Toggleable</string>
141+
<string name="cat_button_group_runtime_add">Add</string>
142+
<string name="cat_button_group_runtime_remove">Remove</string>
140143
</resources>

lib/java/com/google/android/material/button/MaterialButtonGroup.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ void onButtonWidthChanged(@NonNull MaterialButton button, int increaseSize) {
476476
* buttons,
477477
*/
478478
private void adjustChildSizeChange() {
479-
if (buttonSizeChange == null) {
479+
if (buttonSizeChange == null || getChildCount() == 0) {
480480
return;
481481
}
482482
int firstVisibleChildIndex = getFirstVisibleChildIndex();

0 commit comments

Comments
 (0)