0
# Buttons and Selection Controls
1
2
Core input controls including buttons, chips, checkboxes, radio buttons, and switches with Material Design styling and behavior. These components provide consistent interaction patterns following Material Design principles.
3
4
## Capabilities
5
6
### Material Button
7
8
A Material Design button with extensive customization options including icons, stroke styling, and corner radius.
9
10
```java { .api }
11
public class MaterialButton extends AppCompatButton {
12
public MaterialButton(Context context);
13
public MaterialButton(Context context, AttributeSet attrs);
14
15
// Icon management
16
public void setIcon(Drawable icon);
17
public Drawable getIcon();
18
public void setIconTint(ColorStateList iconTint);
19
public ColorStateList getIconTint();
20
public void setIconSize(int iconSize);
21
public int getIconSize();
22
public void setIconGravity(int iconGravity);
23
public int getIconGravity();
24
25
// Stroke and corners
26
public void setStrokeColor(ColorStateList strokeColor);
27
public ColorStateList getStrokeColor();
28
public void setStrokeWidth(int strokeWidth);
29
public int getStrokeWidth();
30
public void setCornerRadius(int cornerRadius);
31
public int getCornerRadius();
32
33
// Ripple effect
34
public void setRippleColor(ColorStateList rippleColor);
35
public ColorStateList getRippleColor();
36
37
// Checkable behavior
38
public void setCheckable(boolean checkable);
39
public boolean isCheckable();
40
public void setChecked(boolean checked);
41
public boolean isChecked();
42
public void toggle();
43
44
// Listeners
45
public void addOnCheckedChangeListener(OnCheckedChangeListener listener);
46
public void removeOnCheckedChangeListener(OnCheckedChangeListener listener);
47
48
public interface OnCheckedChangeListener {
49
void onCheckedChanged(MaterialButton button, boolean isChecked);
50
}
51
}
52
```
53
54
**Usage Example:**
55
56
```java
57
MaterialButton button = new MaterialButton(context);
58
button.setText("Save");
59
button.setIcon(ContextCompat.getDrawable(context, R.drawable.ic_save));
60
button.setIconGravity(MaterialButton.ICON_GRAVITY_TEXT_START);
61
button.setStrokeColor(ColorStateList.valueOf(Color.BLUE));
62
button.setStrokeWidth(2);
63
button.setCornerRadius(8);
64
button.setOnClickListener(v -> saveData());
65
```
66
67
### Material Button Toggle Group
68
69
A container for grouping related MaterialButtons with single or multiple selection support.
70
71
```java { .api }
72
public class MaterialButtonToggleGroup extends LinearLayout {
73
public MaterialButtonToggleGroup(Context context);
74
public MaterialButtonToggleGroup(Context context, AttributeSet attrs);
75
76
// Selection management
77
public void check(@IdRes int id);
78
public void uncheck(@IdRes int id);
79
public void clearChecked();
80
public int getCheckedButtonId();
81
public List<Integer> getCheckedButtonIds();
82
83
// Selection behavior
84
public void setSingleSelection(boolean singleSelection);
85
public boolean isSingleSelection();
86
public void setSelectionRequired(boolean selectionRequired);
87
public boolean isSelectionRequired();
88
89
// Listeners
90
public void addOnButtonCheckedListener(OnButtonCheckedListener listener);
91
public void removeOnButtonCheckedListener(OnButtonCheckedListener listener);
92
93
public interface OnButtonCheckedListener {
94
void onButtonChecked(MaterialButtonToggleGroup group, @IdRes int checkedId, boolean isChecked);
95
}
96
}
97
```
98
99
### Material Split Button
100
101
A specialized container for creating split buttons with a main action and a secondary dropdown action.
102
103
```java { .api }
104
public class MaterialSplitButton extends MaterialButtonGroup {
105
public MaterialSplitButton(Context context);
106
public MaterialSplitButton(Context context, AttributeSet attrs);
107
108
// Inherited from MaterialButtonGroup
109
public void addView(View child, int index, ViewGroup.LayoutParams params);
110
public void setChildrenDrawingOrderEnabled(boolean enabled);
111
public boolean isChildrenDrawingOrderEnabled();
112
113
// Layout and appearance inherited from parent classes
114
public void setOrientation(int orientation);
115
public int getOrientation();
116
}
117
```
118
119
**Usage Example:**
120
121
```java
122
MaterialSplitButton splitButton = new MaterialSplitButton(context);
123
124
// Main action button
125
MaterialButton mainButton = new MaterialButton(context);
126
mainButton.setText("Save");
127
mainButton.setIcon(ContextCompat.getDrawable(context, R.drawable.ic_save));
128
mainButton.setOnClickListener(v -> saveDocument());
129
130
// Dropdown button
131
MaterialButton dropdownButton = new MaterialButton(context);
132
dropdownButton.setIcon(ContextCompat.getDrawable(context, R.drawable.ic_arrow_drop_down));
133
dropdownButton.setOnClickListener(v -> showSaveOptions());
134
135
splitButton.addView(mainButton);
136
splitButton.addView(dropdownButton);
137
```
138
139
### Floating Action Button
140
141
A circular button that performs primary actions in an application.
142
143
```java { .api }
144
public class FloatingActionButton extends VisibilityAwareImageButton implements ExpandableWidget {
145
public FloatingActionButton(Context context);
146
public FloatingActionButton(Context context, AttributeSet attrs);
147
148
// Size management
149
public void setSize(int size);
150
public int getSize();
151
152
// Image and tint
153
public void setSupportImageTintList(ColorStateList tint);
154
public ColorStateList getSupportImageTintList();
155
public void setSupportImageTintMode(PorterDuff.Mode tintMode);
156
public PorterDuff.Mode getSupportImageTintMode();
157
158
// Background
159
public void setBackgroundTintList(ColorStateList tint);
160
public ColorStateList getBackgroundTintList();
161
public void setBackgroundTintMode(PorterDuff.Mode tintMode);
162
public PorterDuff.Mode getBackgroundTintMode();
163
164
// Ripple effect
165
public void setRippleColor(@ColorInt int color);
166
public void setRippleColor(ColorStateList rippleColor);
167
public ColorStateList getRippleColor();
168
169
// Visibility animations
170
public void show();
171
public void show(OnVisibilityChangedListener listener);
172
public void hide();
173
public void hide(OnVisibilityChangedListener listener);
174
175
// Expandable behavior
176
public boolean setExpanded(boolean expanded);
177
public boolean isExpanded();
178
179
// Elevation
180
public void setCompatElevation(float elevation);
181
public float getCompatElevation();
182
public void setCompatHoveredFocusedTranslationZ(float translationZ);
183
public float getCompatHoveredFocusedTranslationZ();
184
public void setCompatPressedTranslationZ(float translationZ);
185
public float getCompatPressedTranslationZ();
186
187
public abstract static class OnVisibilityChangedListener {
188
public void onShown(FloatingActionButton fab);
189
public void onHidden(FloatingActionButton fab);
190
}
191
}
192
```
193
194
**Constants:**
195
```java { .api }
196
public static final int SIZE_MINI = 1;
197
public static final int SIZE_NORMAL = 0;
198
public static final int SIZE_AUTO = -1;
199
```
200
201
### Extended Floating Action Button
202
203
An extended FAB with both icon and text.
204
205
```java { .api }
206
public class ExtendedFloatingActionButton extends MaterialButton implements ExpandableWidget {
207
public ExtendedFloatingActionButton(Context context);
208
public ExtendedFloatingActionButton(Context context, AttributeSet attrs);
209
210
// Text and icon
211
public void setText(CharSequence text);
212
public CharSequence getText();
213
public void setIcon(Drawable icon);
214
public Drawable getIcon();
215
public void setIconTint(ColorStateList iconTint);
216
public ColorStateList getIconTint();
217
218
// Extended state
219
public void extend();
220
public void extend(OnChangedCallback callback);
221
public void shrink();
222
public void shrink(OnChangedCallback callback);
223
public boolean isExtended();
224
public void setExtended(boolean extended);
225
226
// Visibility
227
public void show();
228
public void show(OnChangedCallback callback);
229
public void hide();
230
public void hide(OnChangedCallback callback);
231
232
public abstract static class OnChangedCallback {
233
public void onShown(ExtendedFloatingActionButton extendedFab);
234
public void onHidden(ExtendedFloatingActionButton extendedFab);
235
public void onExtended(ExtendedFloatingActionButton extendedFab);
236
public void onShrunken(ExtendedFloatingActionButton extendedFab);
237
}
238
}
239
```
240
241
### Chip
242
243
A compact element representing an input, attribute, or action.
244
245
```java { .api }
246
public class Chip extends AppCompatCheckBox implements ChipDrawable.Delegate {
247
public Chip(Context context);
248
public Chip(Context context, AttributeSet attrs);
249
250
// Text
251
public void setText(CharSequence text);
252
public CharSequence getText();
253
254
// Chip icon
255
public void setChipIcon(Drawable chipIcon);
256
public Drawable getChipIcon();
257
public void setChipIconTint(ColorStateList chipIconTint);
258
public ColorStateList getChipIconTint();
259
public void setChipIconSize(float chipIconSize);
260
public float getChipIconSize();
261
262
// Close icon
263
public void setCloseIcon(Drawable closeIcon);
264
public Drawable getCloseIcon();
265
public void setCloseIconTint(ColorStateList closeIconTint);
266
public ColorStateList getCloseIconTint();
267
public void setCloseIconSize(float closeIconSize);
268
public float getCloseIconSize();
269
public void setOnCloseIconClickListener(View.OnClickListener listener);
270
public View.OnClickListener getOnCloseIconClickListener();
271
272
// Appearance
273
public void setChipBackgroundColor(ColorStateList chipBackgroundColor);
274
public ColorStateList getChipBackgroundColor();
275
public void setChipStrokeColor(ColorStateList chipStrokeColor);
276
public ColorStateList getChipStrokeColor();
277
public void setChipStrokeWidth(float chipStrokeWidth);
278
public float getChipStrokeWidth();
279
public void setChipCornerRadius(float chipCornerRadius);
280
public float getChipCornerRadius();
281
public void setChipMinHeight(float chipMinHeight);
282
public float getChipMinHeight();
283
284
// Ripple
285
public void setRippleColor(ColorStateList rippleColor);
286
public ColorStateList getRippleColor();
287
288
// Checkable behavior
289
public void setCheckable(boolean checkable);
290
public boolean isCheckable();
291
public void setChecked(boolean checked);
292
public boolean isChecked();
293
}
294
```
295
296
### Chip Group
297
298
A container for a set of chips with single or multiple selection.
299
300
```java { .api }
301
public class ChipGroup extends ViewGroup {
302
public ChipGroup(Context context);
303
public ChipGroup(Context context, AttributeSet attrs);
304
305
// Spacing
306
public void setChipSpacing(int chipSpacing);
307
public void setChipSpacingHorizontal(int chipSpacingHorizontal);
308
public void setChipSpacingVertical(int chipSpacingVertical);
309
public int getChipSpacing();
310
public int getChipSpacingHorizontal();
311
public int getChipSpacingVertical();
312
313
// Layout
314
public void setSingleLine(boolean singleLine);
315
public boolean isSingleLine();
316
317
// Selection
318
public void setSingleSelection(boolean singleSelection);
319
public void setSingleSelection(@BoolRes int id);
320
public boolean isSingleSelection();
321
public void setSelectionRequired(boolean selectionRequired);
322
public boolean isSelectionRequired();
323
324
// Selection management
325
public void check(@IdRes int id);
326
public void uncheck(@IdRes int id);
327
public void clearCheck();
328
public int getCheckedChipId();
329
public List<Integer> getCheckedChipIds();
330
331
// Listeners
332
public void setOnCheckedChangeListener(OnCheckedChangeListener listener);
333
334
public interface OnCheckedChangeListener {
335
void onCheckedChanged(ChipGroup group, @IdRes int checkedId);
336
}
337
}
338
```
339
340
### Material CheckBox
341
342
A Material Design themed CheckBox.
343
344
```java { .api }
345
public class MaterialCheckBox extends AppCompatCheckBox {
346
public MaterialCheckBox(Context context);
347
public MaterialCheckBox(Context context, AttributeSet attrs);
348
349
// Material theming
350
public void setUseMaterialThemeColors(boolean useMaterialThemeColors);
351
public boolean isUseMaterialThemeColors();
352
353
// Button tint
354
public void setButtonTintList(ColorStateList tint);
355
public ColorStateList getButtonTintList();
356
357
// Center positioning
358
public void setCenterIfNoTextEnabled(boolean centerIfNoTextEnabled);
359
public boolean isCenterIfNoTextEnabled();
360
}
361
```
362
363
### Material Radio Button
364
365
A Material Design themed RadioButton.
366
367
```java { .api }
368
public class MaterialRadioButton extends AppCompatRadioButton {
369
public MaterialRadioButton(Context context);
370
public MaterialRadioButton(Context context, AttributeSet attrs);
371
372
// Material theming
373
public void setUseMaterialThemeColors(boolean useMaterialThemeColors);
374
public boolean isUseMaterialThemeColors();
375
}
376
```
377
378
### Switch Material
379
380
A Material Design switch component.
381
382
```java { .api }
383
public class SwitchMaterial extends SwitchCompat {
384
public SwitchMaterial(Context context);
385
public SwitchMaterial(Context context, AttributeSet attrs);
386
387
// Material theming
388
public void setUseMaterialThemeColors(boolean useMaterialThemeColors);
389
public boolean isUseMaterialThemeColors();
390
}
391
```
392
393
## Usage Examples
394
395
### Creating a Button with Icon and Stroke
396
397
```java
398
MaterialButton button = new MaterialButton(context);
399
button.setText("Download");
400
button.setIcon(ContextCompat.getDrawable(context, R.drawable.ic_download));
401
button.setIconGravity(MaterialButton.ICON_GRAVITY_TEXT_START);
402
button.setStrokeColor(ColorStateList.valueOf(getColor(R.color.primary)));
403
button.setStrokeWidth(2);
404
button.setCornerRadius(16);
405
button.setOnClickListener(v -> startDownload());
406
```
407
408
### Creating a Toggle Button Group
409
410
```java
411
MaterialButtonToggleGroup toggleGroup = new MaterialButtonToggleGroup(context);
412
toggleGroup.setSingleSelection(true);
413
toggleGroup.setSelectionRequired(true);
414
415
MaterialButton option1 = new MaterialButton(context);
416
option1.setText("Option 1");
417
option1.setId(R.id.option1);
418
419
MaterialButton option2 = new MaterialButton(context);
420
option2.setText("Option 2");
421
option2.setId(R.id.option2);
422
423
toggleGroup.addView(option1);
424
toggleGroup.addView(option2);
425
toggleGroup.check(R.id.option1);
426
427
toggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
428
if (isChecked) {
429
handleOptionSelected(checkedId);
430
}
431
});
432
```
433
434
### Creating a Chip Group for Filtering
435
436
```java
437
ChipGroup chipGroup = new ChipGroup(context);
438
chipGroup.setSingleSelection(false);
439
440
String[] filters = {"All", "Active", "Completed", "Archived"};
441
for (String filter : filters) {
442
Chip chip = new Chip(context);
443
chip.setText(filter);
444
chip.setCheckable(true);
445
chip.setChipIcon(ContextCompat.getDrawable(context, R.drawable.ic_filter));
446
chipGroup.addView(chip);
447
}
448
449
chipGroup.setOnCheckedChangeListener((group, checkedId) -> {
450
List<Integer> checkedIds = group.getCheckedChipIds();
451
applyFilters(checkedIds);
452
});
453
```
454
455
## Constants
456
457
```java { .api }
458
// MaterialButton icon gravity
459
public static final int ICON_GRAVITY_START = 1;
460
public static final int ICON_GRAVITY_TEXT_START = 2;
461
public static final int ICON_GRAVITY_END = 3;
462
public static final int ICON_GRAVITY_TEXT_END = 4;
463
public static final int ICON_GRAVITY_TOP = 16;
464
public static final int ICON_GRAVITY_TEXT_TOP = 32;
465
466
// FloatingActionButton sizes
467
public static final int SIZE_MINI = 1;
468
public static final int SIZE_NORMAL = 0;
469
public static final int SIZE_AUTO = -1;
470
```