1717
1818import io .material .catalog .R ;
1919
20+ import static android .view .ViewGroup .LayoutParams .WRAP_CONTENT ;
21+
2022import android .annotation .SuppressLint ;
23+ import android .content .Context ;
24+ import android .content .res .TypedArray ;
25+ import android .graphics .drawable .Drawable ;
2126import android .os .Bundle ;
2227import android .view .LayoutInflater ;
2328import android .view .View ;
2934import androidx .annotation .Nullable ;
3035import com .google .android .material .button .MaterialButton ;
3136import com .google .android .material .button .MaterialButtonGroup ;
37+ import com .google .android .material .materialswitch .MaterialSwitch ;
38+ import com .google .android .material .snackbar .Snackbar ;
3239import io .material .catalog .feature .DemoFragment ;
3340
3441/** A fragment that displays a button group demo for adding and removing buttons at runtime. */
3542public class ButtonGroupRuntimeDemoFragment extends DemoFragment {
3643
3744 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 ;
3848 private int buttonCount ;
3949 private Button addButton ;
4050 private Button removeButton ;
@@ -52,43 +62,107 @@ public View onCreateDemoView(
5262 View view =
5363 layoutInflater .inflate (
5464 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 );
5568
56- MaterialButtonGroup buttonGroup = view .findViewById (R .id .cat_dynamic_button_group );
5769 addButton = view .findViewById (R .id .cat_add_button );
5870 removeButton = view .findViewById (R .id .cat_remove_button );
59- updateControl ();
6071 addButton .setOnClickListener (
6172 new OnClickListener () {
6273 @ SuppressLint ("SetTextI18n" )
6374 @ Override
6475 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 );
7277 buttonCount ++;
7378 updateControl ();
7479 }
7580 });
7681 removeButton .setOnClickListener (
7782 v -> {
78- buttonGroup .removeViewAt (buttonGroup . getChildCount () - 1 );
83+ buttonGroup .removeViewAt (buttonCount - 1 );
7984 buttonCount --;
8085 updateControl ();
8186 });
8287
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+
83120 return view ;
84121 }
85122
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 ) {
88162 removeButton .setEnabled (false );
89- }else if (buttonCount == MAX_COUNT ){
163+ } else if (buttonCount == MAX_COUNT ) {
90164 addButton .setEnabled (false );
91- }else {
165+ } else {
92166 addButton .setEnabled (true );
93167 removeButton .setEnabled (true );
94168 }
0 commit comments