-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathMaterialButtonGroup.java
787 lines (706 loc) · 28.2 KB
/
MaterialButtonGroup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.button;
import com.google.android.material.R;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
import static java.lang.Math.max;
import static java.lang.Math.min;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
import com.google.android.material.button.MaterialButton.OnPressedChangeListener;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.shape.AbsoluteCornerSize;
import com.google.android.material.shape.CornerSize;
import com.google.android.material.shape.RelativeCornerSize;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.google.android.material.shape.StateListCornerSize;
import com.google.android.material.shape.StateListShapeAppearanceModel;
import com.google.android.material.shape.StateListSizeChange;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* A common container for a set of related {@link MaterialButton}s. The {@link MaterialButton}s in
* this group will be shown on a single line.
*
* <p>This layout currently only supports child views of type {@link MaterialButton}. Buttons can be
* added to this view group via XML, as follows:
*
* <pre>
* <com.google.android.material.button.MaterialButtonGroup
* xmlns:android="http://schemas.android.com/apk/res/android"
* android:id="@+id/toggle_button_group"
* android:layout_width="wrap_content"
* android:layout_height="wrap_content">
*
* <com.google.android.material.button.MaterialButton
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:text="@string/button_label_private"/>
* <com.google.android.material.button.MaterialButton
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:text="@string/button_label_team"/>
* <com.google.android.material.button.MaterialButton
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:text="@string/button_label_everyone"/>
* <com.google.android.material.button.MaterialButton
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:text="@string/button_label_custom"/>
*
* </com.google.android.material.button.MaterialButtonGroup>
* </pre>
*
* <p>Buttons can also be added to this view group programmatically via the {@link #addView(View)}
* methods.
*
* <p>MaterialButtonGroup is a {@link LinearLayout}. Using {@code
* android:layout_width="MATCH_PARENT"} and removing {@code android:insetBottom} {@code
* android:insetTop} on the children is recommended if using {@code VERTICAL}.
*
* <p>For more information, see the <a
* href="https://github.com/material-components/material-components-android/blob/master/docs/components/Button.md">component
* developer guidance</a> and <a href="https://material.io/components/buttons/overview">design
* guidelines</a>.
*/
public class MaterialButtonGroup extends LinearLayout {
private static final String LOG_TAG = "MButtonGroup";
private static final int DEF_STYLE_RES = R.style.Widget_Material3_MaterialButtonGroup;
private final List<ShapeAppearanceModel> originalChildShapeAppearanceModels = new ArrayList<>();
private final List<StateListShapeAppearanceModel> originalChildStateListShapeAppearanceModels =
new ArrayList<>();
private final PressedStateTracker pressedStateTracker = new PressedStateTracker();
private final Comparator<MaterialButton> childOrderComparator =
(v1, v2) -> {
int checked = Boolean.valueOf(v1.isChecked()).compareTo(v2.isChecked());
if (checked != 0) {
return checked;
}
int stateful = Boolean.valueOf(v1.isPressed()).compareTo(v2.isPressed());
if (stateful != 0) {
return stateful;
}
// don't return 0s
return Integer.compare(indexOfChild(v1), indexOfChild(v2));
};
private Integer[] childOrder;
@Nullable StateListCornerSize innerCornerSize;
@Nullable private StateListShapeAppearanceModel groupStateListShapeAppearance;
@Px private int spacing;
@Nullable private StateListSizeChange buttonSizeChange;
private boolean isChildrenShapeInvalidated = true;
public MaterialButtonGroup(@NonNull Context context) {
this(context, null);
}
public MaterialButtonGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.materialButtonGroupStyle);
}
public MaterialButtonGroup(
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
// Ensure we are using the correctly themed context rather than the context that was passed in.
context = getContext();
TypedArray attributes =
ThemeEnforcement.obtainStyledAttributes(
context, attrs, R.styleable.MaterialButtonGroup, defStyleAttr, DEF_STYLE_RES);
if (attributes.hasValue(R.styleable.MaterialButtonGroup_buttonSizeChange)) {
buttonSizeChange =
StateListSizeChange.create(
context, attributes, R.styleable.MaterialButtonGroup_buttonSizeChange);
}
if (attributes.hasValue(R.styleable.MaterialButtonGroup_shapeAppearance)) {
groupStateListShapeAppearance =
StateListShapeAppearanceModel.create(
context, attributes, R.styleable.MaterialButtonGroup_shapeAppearance);
if (groupStateListShapeAppearance == null) {
groupStateListShapeAppearance =
new StateListShapeAppearanceModel.Builder(
ShapeAppearanceModel.builder(
context,
attributes.getResourceId(
R.styleable.MaterialButtonGroup_shapeAppearance, 0),
attributes.getResourceId(
R.styleable.MaterialButtonGroup_shapeAppearanceOverlay, 0))
.build())
.build();
}
}
if (attributes.hasValue(R.styleable.MaterialButtonGroup_innerCornerSize)) {
innerCornerSize =
StateListCornerSize.create(
context,
attributes,
R.styleable.MaterialButtonGroup_innerCornerSize,
new AbsoluteCornerSize(0));
}
spacing = attributes.getDimensionPixelSize(R.styleable.MaterialButtonGroup_android_spacing, 0);
setChildrenDrawingOrderEnabled(true);
setEnabled(attributes.getBoolean(R.styleable.MaterialButtonGroup_android_enabled, true));
attributes.recycle();
}
@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
updateChildOrder();
super.dispatchDraw(canvas);
}
/**
* This override prohibits Views other than {@link MaterialButton} to be added. It also makes
* updates to the add button shape and margins.
*/
@Override
public void addView(@NonNull View child, int index, @Nullable ViewGroup.LayoutParams params) {
if (!(child instanceof MaterialButton)) {
Log.e(LOG_TAG, "Child views must be of type MaterialButton.");
return;
}
// Recover the original layout params of all children before adding the new child.
recoverAllChildrenLayoutParams();
isChildrenShapeInvalidated = true;
super.addView(child, index, params);
MaterialButton buttonChild = (MaterialButton) child;
setGeneratedIdIfNeeded(buttonChild);
buttonChild.setOnPressedChangeListenerInternal(pressedStateTracker);
// Saves original child shape appearance.
originalChildShapeAppearanceModels.add(buttonChild.getShapeAppearanceModel());
originalChildStateListShapeAppearanceModels.add(buttonChild.getStateListShapeAppearanceModel());
// Enable children based on the MaterialButtonToggleGroup own isEnabled
buttonChild.setEnabled(isEnabled());
}
@Override
public void onViewRemoved(View child) {
super.onViewRemoved(child);
if (child instanceof MaterialButton) {
((MaterialButton) child).setOnPressedChangeListenerInternal(null);
}
int indexOfChild = indexOfChild(child);
if (indexOfChild >= 0) {
originalChildShapeAppearanceModels.remove(indexOfChild);
originalChildStateListShapeAppearanceModels.remove(indexOfChild);
}
isChildrenShapeInvalidated = true;
updateChildShapes();
// Recover the original layout params of all children before updating the child layout.
recoverAllChildrenLayoutParams();
adjustChildMarginsAndUpdateLayout();
}
private void recoverAllChildrenLayoutParams(){
for (int i = 0; i < getChildCount(); i++) {
MaterialButton child = getChildButton(i);
child.recoverOriginalLayoutParams();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
updateChildShapes();
adjustChildMarginsAndUpdateLayout();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
recoverAllChildrenLayoutParams();
adjustChildSizeChange();
}
}
/**
* Sets all corner radii override to inner corner size except for leftmost and rightmost corners.
*/
@VisibleForTesting
void updateChildShapes() {
// No need to update shape if no inside corners or outer corners are specified.
if ((innerCornerSize == null && groupStateListShapeAppearance == null)
|| !isChildrenShapeInvalidated) {
return;
}
isChildrenShapeInvalidated = false;
int childCount = getChildCount();
int firstVisibleChildIndex = getFirstVisibleChildIndex();
int lastVisibleChildIndex = getLastVisibleChildIndex();
for (int i = 0; i < childCount; i++) {
MaterialButton button = getChildButton(i);
if (button.getVisibility() == GONE) {
continue;
}
boolean isFirstVisible = i == firstVisibleChildIndex;
boolean isLastVisible = i == lastVisibleChildIndex;
StateListShapeAppearanceModel.Builder originalStateListShapeBuilder =
getOriginalStateListShapeBuilder(isFirstVisible, isLastVisible, i);
// Determines which corners of the original shape should be kept.
boolean isHorizontal = getOrientation() == HORIZONTAL;
boolean isRtl = ViewUtils.isLayoutRtl(this);
int cornerPositionBitsToKeep = 0;
if (isHorizontal) {
// When horizontal (ltr), keeps the left two original corners for the first button.
if (isFirstVisible) {
cornerPositionBitsToKeep |=
StateListShapeAppearanceModel.CORNER_TOP_LEFT
| StateListShapeAppearanceModel.CORNER_BOTTOM_LEFT;
}
// When horizontal (ltr), keeps the right two original corners for the last button.
if (isLastVisible) {
cornerPositionBitsToKeep |=
StateListShapeAppearanceModel.CORNER_TOP_RIGHT
| StateListShapeAppearanceModel.CORNER_BOTTOM_RIGHT;
}
// If rtl, swap the position bits of left corners and right corners.
if (isRtl) {
cornerPositionBitsToKeep =
StateListShapeAppearanceModel.swapCornerPositionRtl(cornerPositionBitsToKeep);
}
} else {
// When vertical, keeps the top two original corners for the first button.
if (isFirstVisible) {
cornerPositionBitsToKeep |=
StateListShapeAppearanceModel.CORNER_TOP_LEFT
| StateListShapeAppearanceModel.CORNER_TOP_RIGHT;
}
// When vertical, keeps the bottom two original corners for the last button.
if (isLastVisible) {
cornerPositionBitsToKeep |=
StateListShapeAppearanceModel.CORNER_BOTTOM_LEFT
| StateListShapeAppearanceModel.CORNER_BOTTOM_RIGHT;
}
}
// Overrides the corners that don't need to keep with unary operator.
int cornerPositionBitsToOverride = ~cornerPositionBitsToKeep;
StateListShapeAppearanceModel newStateListShape =
originalStateListShapeBuilder
.setCornerSizeOverride(innerCornerSize, cornerPositionBitsToOverride)
.build();
if (newStateListShape.isStateful()) {
button.setStateListShapeAppearanceModel(newStateListShape);
} else {
button.setShapeAppearanceModel(
newStateListShape.getDefaultShape(/* withCornerSizeOverrides= */ true));
}
}
}
/**
* Returns a {@link StateListShapeAppearanceModel.Builder} as the original shape of a child
* button.
*
* <p>It takes the group shape, if specified, as the original state list shape for the first and
* last buttons. Otherwise, it takes the state list shape (or build one from the shape appearance
* model, if state list shape is not specified) in the child button.
*
* @param isFirstVisible Whether this is the first visible child button regardless its index.
* @param isLastVisible Whether this is the last visible child button regardless its index.
* @param index The index of the child button.
*/
@NonNull
private StateListShapeAppearanceModel.Builder getOriginalStateListShapeBuilder(
boolean isFirstVisible, boolean isLastVisible, int index) {
StateListShapeAppearanceModel originalStateList =
groupStateListShapeAppearance != null && (isFirstVisible || isLastVisible)
? groupStateListShapeAppearance
: originalChildStateListShapeAppearanceModels.get(index);
// If the state list shape is not specified, creates one from the shape appearance model.
return originalStateList == null
? new StateListShapeAppearanceModel.Builder(originalChildShapeAppearanceModels.get(index))
: originalStateList.toBuilder();
}
/**
* We keep track of which views are pressed and checked to draw them last. This prevents visual
* issues with overlapping strokes.
*/
@Override
protected int getChildDrawingOrder(int childCount, int i) {
if (childOrder == null || i >= childOrder.length) {
Log.w(LOG_TAG, "Child order wasn't updated");
return i;
}
return childOrder[i];
}
/**
* Sets a negative marginStart on all but the first child, if two adjacent children both have a
* stroke width greater than 0. This prevents a double-width stroke from being drawn for two
* adjacent stroked children, and instead draws the adjacent strokes directly on top of each
* other.
*
* <p>The negative margin adjustment amount will be equal to the smaller of the two adjacent
* stroke widths.
*
* <p>Also rearranges children such that they are shown in the correct visual order.
*/
private void adjustChildMarginsAndUpdateLayout() {
int firstVisibleChildIndex = getFirstVisibleChildIndex();
if (firstVisibleChildIndex == -1) {
return;
}
for (int i = firstVisibleChildIndex + 1; i < getChildCount(); i++) {
// Only adjusts margins if both adjacent children are MaterialButtons
MaterialButton currentButton = getChildButton(i);
MaterialButton previousButton = getChildButton(i - 1);
// Calculates the margin adjustment to be the smaller of the two adjacent stroke widths
int smallestStrokeWidth = 0;
if (spacing <= 0) {
smallestStrokeWidth = min(currentButton.getStrokeWidth(), previousButton.getStrokeWidth());
}
LayoutParams params = buildLayoutParams(currentButton);
if (getOrientation() == HORIZONTAL) {
params.setMarginEnd(0);
params.setMarginStart(spacing - smallestStrokeWidth);
params.topMargin = 0;
} else {
params.bottomMargin = 0;
params.topMargin = spacing - smallestStrokeWidth;
params.setMarginStart(0);
}
currentButton.setLayoutParams(params);
}
resetChildMargins(firstVisibleChildIndex);
}
private void resetChildMargins(int childIndex) {
if (getChildCount() == 0 || childIndex == -1) {
return;
}
MaterialButton currentButton = getChildButton(childIndex);
LayoutParams params = (LayoutParams) currentButton.getLayoutParams();
if (getOrientation() == VERTICAL) {
params.topMargin = 0;
params.bottomMargin = 0;
return;
}
params.setMarginEnd(0);
params.setMarginStart(0);
params.leftMargin = 0;
params.rightMargin = 0;
}
void onButtonWidthChanged(@NonNull MaterialButton button, int increaseSize) {
int buttonIndex = indexOfChild(button);
if (buttonIndex < 0) {
return;
}
MaterialButton prevVisibleButton = getPrevVisibleChildButton(buttonIndex);
MaterialButton nextVisibleButton = getNextVisibleChildButton(buttonIndex);
if (prevVisibleButton == null && nextVisibleButton == null) {
return;
}
if (prevVisibleButton == null) {
nextVisibleButton.setDisplayedWidthDecrease(increaseSize);
}
if (nextVisibleButton == null) {
prevVisibleButton.setDisplayedWidthDecrease(increaseSize);
}
if (prevVisibleButton != null && nextVisibleButton != null) {
// If there are two neighbors, each neighbor will absorb half of the expanded amount.
prevVisibleButton.setDisplayedWidthDecrease(increaseSize / 2);
// We want to avoid one pixel missing due to the casting, when increaseSize is odd.
nextVisibleButton.setDisplayedWidthDecrease((increaseSize + 1) / 2);
}
}
/**
* Adjusts the max amount of size to expand for each child button. So that it won't squeeze its
* neighbors too much to cause text truncation; and the expansion amount per edge is same for all
* buttons,
*/
private void adjustChildSizeChange() {
if (buttonSizeChange == null) {
return;
}
int firstVisibleChildIndex = getFirstVisibleChildIndex();
int lastVisibleChildIndex = getLastVisibleChildIndex();
int widthIncreaseOnSingleEdge = Integer.MAX_VALUE;
for (int i = firstVisibleChildIndex; i <= lastVisibleChildIndex; i++) {
if (!isChildVisible(i)) {
continue;
}
// Calculates the allowed width increase for each child button with consideration of the max
// allowed width decrease of its neighbors.
int widthIncrease = getButtonAllowedWidthIncrease(i);
// If the button expands on both edges, the width increase on each edge should be half of
// the total width increase. Calculates the minimum width increase on each edge, so that all
// buttons won't squeeze their neighbors too much.
widthIncreaseOnSingleEdge =
min(
widthIncreaseOnSingleEdge,
i != firstVisibleChildIndex && i != lastVisibleChildIndex
? widthIncrease / 2
: widthIncrease);
}
for (int i = firstVisibleChildIndex; i <= lastVisibleChildIndex; i++) {
if (!isChildVisible(i)) {
continue;
}
getChildButton(i).setSizeChange(buttonSizeChange);
// If the button expands on both edges, the total width increase should be double of the
// width increase on each edge.
getChildButton(i)
.setWidthChangeMax(
i == firstVisibleChildIndex || i == lastVisibleChildIndex
? widthIncreaseOnSingleEdge
: widthIncreaseOnSingleEdge * 2);
}
}
/**
* Returns the allowed width increase for a child button.
*
* <p>The allowed width increase is the smaller amount of the max width increase of the button in
* all states and the total allowed width decrease of its neighbors.
*/
private int getButtonAllowedWidthIncrease(int index) {
if (!isChildVisible(index) || buttonSizeChange == null) {
return 0;
}
MaterialButton currentButton = getChildButton(index);
int widthIncrease = max(0, buttonSizeChange.getMaxWidthChange(currentButton.getWidth()));
// Checking neighbors' allowed width decrease.
MaterialButton prevVisibleButton = getPrevVisibleChildButton(index);
int prevButtonAllowedWidthDecrease =
prevVisibleButton == null ? 0 : prevVisibleButton.getAllowedWidthDecrease();
MaterialButton nextVisibleButton = getNextVisibleChildButton(index);
int nextButtonAllowedWidthDecrease =
nextVisibleButton == null ? 0 : nextVisibleButton.getAllowedWidthDecrease();
return min(widthIncrease, prevButtonAllowedWidthDecrease + nextButtonAllowedWidthDecrease);
}
// ================ Getters and setters ===================
/**
* Returns the {@link StateListSizeChange} of child buttons on state changes.
*
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
@Nullable
public StateListSizeChange getButtonSizeChange() {
return buttonSizeChange;
}
/**
* Sets the {@link StateListSizeChange} of child buttons on state changes.
*
* @param buttonSizeChange The new {@link StateListSizeChange} of child buttons.
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
public void setButtonSizeChange(@NonNull StateListSizeChange buttonSizeChange) {
if (this.buttonSizeChange != buttonSizeChange) {
this.buttonSizeChange = buttonSizeChange;
adjustChildSizeChange();
requestLayout();
invalidate();
}
}
/** Returns the spacing (in pixels) between each button in the group. */
@Px
public int getSpacing() {
return spacing;
}
/**
* Sets the spacing between each button in the group.
*
* @param spacing the spacing (in pixels) between each button in the group
*/
public void setSpacing(@Px int spacing) {
this.spacing = spacing;
invalidate();
requestLayout();
}
/** Returns the inner corner size of the group. */
@NonNull
public CornerSize getInnerCornerSize() {
return innerCornerSize.getDefaultCornerSize();
}
/**
* Sets the inner corner size of the group.
*
* <p>Can set as an {@link AbsoluteCornerSize} or {@link RelativeCornerSize}. Don't set relative
* corner size larger than 50% or absolute corner size larger than half height to avoid corner
* overlapping.
*
* @param cornerSize the inner corner size of the group
*/
public void setInnerCornerSize(@NonNull CornerSize cornerSize) {
innerCornerSize = StateListCornerSize.create(cornerSize);
isChildrenShapeInvalidated = true;
updateChildShapes();
invalidate();
}
/**
* Returns the inner corner size state list of the group.
*
* @hide
*/
@NonNull
@RestrictTo(Scope.LIBRARY_GROUP)
public StateListCornerSize getInnerCornerSizeStateList() {
return innerCornerSize;
}
/**
* Sets the inner corner size state list of the group.
*
* <p>Can set as an {@link StateListCornerSize}. Don't set relative corner size larger than 50% or
* absolute corner size larger than half height to the corner size in any state to avoid corner
* overlapping.
*
* @param cornerSizeStateList the inner corner size state list of the group
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
public void setInnerCornerSizeStateList(@NonNull StateListCornerSize cornerSizeStateList) {
innerCornerSize = cornerSizeStateList;
isChildrenShapeInvalidated = true;
updateChildShapes();
invalidate();
}
/** Returns the {@link ShapeAppearanceModel} of the group. */
@Nullable
public ShapeAppearanceModel getShapeAppearance() {
return groupStateListShapeAppearance == null
? null
: groupStateListShapeAppearance.getDefaultShape(/* withCornerSizeOverrides= */ true);
}
/**
* Sets the {@link ShapeAppearanceModel} of the group.
*
* @param shapeAppearance The new {@link ShapeAppearanceModel} of the group.
*/
public void setShapeAppearance(@Nullable ShapeAppearanceModel shapeAppearance) {
groupStateListShapeAppearance =
new StateListShapeAppearanceModel.Builder(shapeAppearance).build();
isChildrenShapeInvalidated = true;
updateChildShapes();
invalidate();
}
/**
* Returns the {@link StateListShapeAppearanceModel} of the group.
*
* @hide
*/
@Nullable
@RestrictTo(Scope.LIBRARY_GROUP)
public StateListShapeAppearanceModel getStateListShapeAppearance() {
return groupStateListShapeAppearance;
}
/**
* Sets the {@link StateListShapeAppearanceModel} of the group.
*
* @param stateListShapeAppearance The new {@link StateListShapeAppearanceModel} of the group.
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
public void setStateListShapeAppearance(
@Nullable StateListShapeAppearanceModel stateListShapeAppearance) {
groupStateListShapeAppearance = stateListShapeAppearance;
isChildrenShapeInvalidated = true;
updateChildShapes();
invalidate();
}
// ================ Helper functions ===================
@NonNull
MaterialButton getChildButton(int index) {
return (MaterialButton) getChildAt(index);
}
@NonNull
LinearLayout.LayoutParams buildLayoutParams(@NonNull View child) {
ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
if (layoutParams instanceof LinearLayout.LayoutParams) {
return (LayoutParams) layoutParams;
}
return new LayoutParams(layoutParams.width, layoutParams.height);
}
private int getFirstVisibleChildIndex() {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
if (isChildVisible(i)) {
return i;
}
}
return -1;
}
private int getLastVisibleChildIndex() {
int childCount = getChildCount();
for (int i = childCount - 1; i >= 0; i--) {
if (isChildVisible(i)) {
return i;
}
}
return -1;
}
private boolean isChildVisible(int i) {
View child = getChildAt(i);
return child.getVisibility() != View.GONE;
}
private void setGeneratedIdIfNeeded(@NonNull MaterialButton materialButton) {
// Generates an ID if none is set, for relative positioning purposes
if (materialButton.getId() == View.NO_ID) {
materialButton.setId(View.generateViewId());
}
}
@Nullable
private MaterialButton getNextVisibleChildButton(int index) {
int childCount = getChildCount();
for (int i = index + 1; i < childCount; i++) {
if (isChildVisible(i)) {
return getChildButton(i);
}
}
return null;
}
@Nullable
private MaterialButton getPrevVisibleChildButton(int index) {
for (int i = index - 1; i >= 0; i--) {
if (isChildVisible(i)) {
return getChildButton(i);
}
}
return null;
}
private void updateChildOrder() {
final SortedMap<MaterialButton, Integer> viewToIndexMap = new TreeMap<>(childOrderComparator);
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
viewToIndexMap.put(getChildButton(i), i);
}
childOrder = viewToIndexMap.values().toArray(new Integer[0]);
}
/**
* Enables this {@link MaterialButtonGroup} and all its {@link MaterialButton} children
*
* @param enabled boolean to setEnable {@link MaterialButtonGroup}
*/
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
// Enable or disable child buttons
for (int i = 0; i < getChildCount(); i++) {
MaterialButton childButton = getChildButton(i);
childButton.setEnabled(enabled);
}
}
private class PressedStateTracker implements OnPressedChangeListener {
@Override
public void onPressedChanged(@NonNull MaterialButton button, boolean isPressed) {
invalidate();
}
}
}