Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/java/com/google/android/material/button/MaterialButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
Expand Down Expand Up @@ -1346,11 +1347,19 @@ public void toggle() {

@Override
public boolean performClick() {
if (isEnabled() && materialButtonHelper.isToggleCheckedStateOnClick()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pubiqq thanks for the PR - can you explain why the isEnabled() check is removed? is it because perfomClick() should already never be called if the button is disabled? if so, I agree that the user can't click a disabled button, but it's possible that this method is called programmatically on a disabled button, so maybe we should keep it and do isEnabled() && isCheckable() && materialButtonHelper.isToggleCheckedStateOnClick()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Components such as android.widget.Button and android.widget.CheckBox perform click actions when the performClick method is called, even if they're disabled. I see no reason why the MaterialButton should be any different.

Also, the original implementation is incorrect because clicking on the disabled button works inconsistently; it doesn't invoke the toggle() method, but still performs click actions.

final boolean checkable = isCheckable() && materialButtonHelper.isToggleCheckedStateOnClick();
if (checkable) {
toggle();
}

return super.performClick();
final boolean handled = super.performClick();
if (checkable && !handled) {
// View only makes a sound effect if the onClickListener was
// called, so for checkable button we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}

return handled;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ void onButtonCheckedStateChanged(@NonNull MaterialButton button, boolean isCheck
if (skipCheckedStateTracker) {
return;
}
button.playSoundEffect(SoundEffectConstants.CLICK);
checkInternal(button.getId(), isChecked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ public void addView(@NonNull View child, int index, @Nullable ViewGroup.LayoutPa

buttonChild.addOnCheckedChangeListener(
(button, isChecked) -> {
// Play sound effect when checked state changes.
button.playSoundEffect(SoundEffectConstants.CLICK);
// Update content description when checked state changes.
ViewCompat.setStateDescription(
buttonChild,
Expand Down