17
17
18
18
import io .material .catalog .R ;
19
19
20
+ import static android .view .ViewGroup .LayoutParams .WRAP_CONTENT ;
21
+
20
22
import android .annotation .SuppressLint ;
23
+ import android .content .Context ;
24
+ import android .content .res .TypedArray ;
25
+ import android .graphics .drawable .Drawable ;
21
26
import android .os .Bundle ;
22
27
import android .view .LayoutInflater ;
23
28
import android .view .View ;
29
34
import androidx .annotation .Nullable ;
30
35
import com .google .android .material .button .MaterialButton ;
31
36
import com .google .android .material .button .MaterialButtonGroup ;
37
+ import com .google .android .material .materialswitch .MaterialSwitch ;
38
+ import com .google .android .material .snackbar .Snackbar ;
32
39
import io .material .catalog .feature .DemoFragment ;
33
40
34
41
/** A fragment that displays a button group demo for adding and removing buttons at runtime. */
35
42
public class ButtonGroupRuntimeDemoFragment extends DemoFragment {
36
43
37
44
private static final int MAX_COUNT = 10 ;
45
+ private final String [] labels = new String [MAX_COUNT ];
46
+ private final Drawable [] icons = new Drawable [MAX_COUNT ];
47
+ private MaterialButtonGroup buttonGroup ;
38
48
private int buttonCount ;
39
49
private Button addButton ;
40
50
private Button removeButton ;
@@ -52,43 +62,107 @@ public View onCreateDemoView(
52
62
View view =
53
63
layoutInflater .inflate (
54
64
R .layout .cat_buttons_group_runtime_fragment , viewGroup , /* attachToRoot= */ false );
65
+ Context context = view .getContext ();
66
+
67
+ buttonGroup = view .findViewById (R .id .cat_dynamic_button_group_label_only );
55
68
56
- MaterialButtonGroup buttonGroup = view .findViewById (R .id .cat_dynamic_button_group );
57
69
addButton = view .findViewById (R .id .cat_add_button );
58
70
removeButton = view .findViewById (R .id .cat_remove_button );
59
- updateControl ();
60
71
addButton .setOnClickListener (
61
72
new OnClickListener () {
62
73
@ SuppressLint ("SetTextI18n" )
63
74
@ Override
64
75
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 ));
76
+ addButton (context );
72
77
buttonCount ++;
73
78
updateControl ();
74
79
}
75
80
});
76
81
removeButton .setOnClickListener (
77
82
v -> {
78
- buttonGroup .removeViewAt (buttonGroup . getChildCount () - 1 );
83
+ buttonGroup .removeViewAt (buttonCount - 1 );
79
84
buttonCount --;
80
85
updateControl ();
81
86
});
82
87
88
+ updateControl ();
89
+ loadResources ();
90
+
91
+ MaterialSwitch lastCheckedSwitch = view .findViewById (R .id .last_checked_switch );
92
+ lastCheckedSwitch .setOnCheckedChangeListener (
93
+ (buttonView , isChecked ) -> {
94
+ if (ensureAtLeastOneButton ()) {
95
+ MaterialButton lastButton = (MaterialButton ) buttonGroup .getChildAt (buttonCount - 1 );
96
+ lastButton .setChecked (isChecked );
97
+ lastButton .refreshDrawableState ();
98
+ }
99
+ });
100
+ MaterialSwitch lastCheckableSwitch = view .findViewById (R .id .last_checkable_switch );
101
+ lastCheckableSwitch .setOnCheckedChangeListener (
102
+ (buttonView , isChecked ) -> {
103
+ if (ensureAtLeastOneButton ()) {
104
+ MaterialButton lastButton = (MaterialButton ) buttonGroup .getChildAt (buttonCount - 1 );
105
+ lastButton .setCheckable (isChecked );
106
+ lastButton .refreshDrawableState ();
107
+ lastCheckedSwitch .setEnabled (isChecked );
108
+ }
109
+ });
110
+ MaterialSwitch enableSwitch = view .findViewById (R .id .last_enabled_switch );
111
+ enableSwitch .setOnCheckedChangeListener (
112
+ (buttonView , isChecked ) -> {
113
+ if (ensureAtLeastOneButton ()) {
114
+ MaterialButton lastButton = (MaterialButton ) buttonGroup .getChildAt (buttonCount - 1 );
115
+ lastButton .setEnabled (isChecked );
116
+ lastButton .refreshDrawableState ();
117
+ }
118
+ });
119
+
83
120
return view ;
84
121
}
85
122
86
- private void updateControl (){
87
- if (buttonCount == 0 ){
123
+ private boolean ensureAtLeastOneButton () {
124
+ if (buttonCount == 0 ) {
125
+ Snackbar .make (buttonGroup , "Add a button first." , Snackbar .LENGTH_LONG ).show ();
126
+ return false ;
127
+ }
128
+ return true ;
129
+ }
130
+
131
+ private void addButton (@ NonNull Context context ) {
132
+ MaterialButton button = new MaterialButton (context );
133
+ button .setText (labels [buttonCount ]);
134
+ button .setIcon (icons [buttonCount ]);
135
+ button .setCheckable (true );
136
+ button .setMaxLines (1 );
137
+ buttonGroup .addView (button , new LayoutParams (WRAP_CONTENT , WRAP_CONTENT , 1 ));
138
+ MaterialButtonGroup .LayoutParams lp =
139
+ (MaterialButtonGroup .LayoutParams ) button .getLayoutParams ();
140
+ lp .overflowText = labels [buttonCount ];
141
+ lp .overflowIcon = icons [buttonCount ];
142
+ }
143
+
144
+ private void loadResources () {
145
+ TypedArray labelsRes = getResources ().obtainTypedArray (R .array .cat_button_group_dynamic_labels );
146
+ for (int i = 0 ; i < MAX_COUNT ; i ++) {
147
+ labels [i ] = labelsRes .getString (i % labelsRes .length ());
148
+ }
149
+ labelsRes .recycle ();
150
+ TypedArray iconsRes = getResources ().obtainTypedArray (R .array .cat_button_group_dynamic_icons );
151
+ for (int i = 0 ; i < MAX_COUNT ; i ++) {
152
+ int iconId = iconsRes .getResourceId (i % iconsRes .length (), 0 );
153
+ if (iconId != 0 ) {
154
+ icons [i ] = getResources ().getDrawable (iconId );
155
+ }
156
+ }
157
+ iconsRes .recycle ();
158
+ }
159
+
160
+ private void updateControl () {
161
+ if (buttonCount == 0 ) {
88
162
removeButton .setEnabled (false );
89
- }else if (buttonCount == MAX_COUNT ){
163
+ } else if (buttonCount == MAX_COUNT ) {
90
164
addButton .setEnabled (false );
91
- }else {
165
+ } else {
92
166
addButton .setEnabled (true );
93
167
removeButton .setEnabled (true );
94
168
}
0 commit comments