Skip to content

Commit 0c5b17a

Browse files
committed
[Button] Fixed a bug that checked state in XML was not respected.
PiperOrigin-RevId: 755123809
1 parent 603f1ef commit 0c5b17a

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ public MaterialButton(@NonNull Context context, @Nullable AttributeSet attrs, in
309309
materialButtonHelper = new MaterialButtonHelper(this, shapeAppearanceModel);
310310
materialButtonHelper.loadFromAttributes(attributes);
311311

312+
// Sets the checked state after the MaterialButtonHelper is initialized.
313+
setCheckedInternal(attributes.getBoolean(R.styleable.MaterialButton_android_checked, false));
314+
312315
if (stateListShapeAppearanceModel != null) {
313316
materialButtonHelper.setCornerSpringForce(createSpringForce());
314317
materialButtonHelper.setStateListShapeAppearanceModel(stateListShapeAppearanceModel);
@@ -1296,6 +1299,10 @@ public void clearOnCheckedChangeListeners() {
12961299

12971300
@Override
12981301
public void setChecked(boolean checked) {
1302+
setCheckedInternal(checked);
1303+
}
1304+
1305+
private void setCheckedInternal(boolean checked) {
12991306
if (isCheckable() && this.checked != checked) {
13001307
this.checked = checked;
13011308

lib/java/com/google/android/material/button/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
<declare-styleable name="MaterialButton">
5252
<!-- Whether the button can be checked. -->
53+
<attr name="android:checked" />
5354
<attr name="android:checkable" />
5455
<attr name="android:insetLeft" />
5556
<attr name="android:insetRight" />

0 commit comments

Comments
 (0)