|
49 | 49 | import androidx.appcompat.widget.AppCompatButton; |
50 | 50 | import android.util.AttributeSet; |
51 | 51 | import android.util.Log; |
| 52 | +import android.view.accessibility.AccessibilityNodeInfo; |
| 53 | +import android.widget.Checkable; |
52 | 54 | import java.lang.annotation.Retention; |
53 | 55 | import java.lang.annotation.RetentionPolicy; |
54 | 56 |
|
|
88 | 90 | * <p>Specify the radius of all four corners of the button using the {@link R.attr#cornerRadius |
89 | 91 | * app:cornerRadius} attribute. |
90 | 92 | */ |
91 | | -public class MaterialButton extends AppCompatButton { |
| 93 | +public class MaterialButton extends AppCompatButton implements Checkable { |
| 94 | + |
| 95 | + /** Interface definition for a callback to be invoked when the button checked state changes. */ |
| 96 | + public interface OnCheckedChangeListener { |
| 97 | + /** |
| 98 | + * Called when the checked state of a MaterialButton has changed. |
| 99 | + * |
| 100 | + * @param button The MaterialButton whose state has changed. |
| 101 | + * @param isChecked The new checked state of MaterialButton. |
| 102 | + */ |
| 103 | + void onCheckedChanged(MaterialButton button, boolean isChecked); |
| 104 | + } |
| 105 | + |
| 106 | + private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked}; |
92 | 107 |
|
93 | 108 | /** |
94 | 109 | * Gravity used to position the icon at the start of the view. |
@@ -123,6 +138,9 @@ public class MaterialButton extends AppCompatButton { |
123 | 138 | private Drawable icon; |
124 | 139 | @Px private int iconSize; |
125 | 140 | @Px private int iconLeft; |
| 141 | + private boolean checked = false; |
| 142 | + private boolean broadcasting = false; |
| 143 | + @Nullable private OnCheckedChangeListener onCheckedChangeListener; |
126 | 144 |
|
127 | 145 | @IconGravity private int iconGravity; |
128 | 146 |
|
@@ -168,6 +186,14 @@ public MaterialButton(Context context, AttributeSet attrs, int defStyleAttr) { |
168 | 186 | updateIcon(); |
169 | 187 | } |
170 | 188 |
|
| 189 | + @Override |
| 190 | + public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 191 | + super.onInitializeAccessibilityNodeInfo(info); |
| 192 | + info.setClassName(MaterialButton.class.getName()); |
| 193 | + info.setCheckable(isCheckable()); |
| 194 | + info.setClickable(isClickable()); |
| 195 | + } |
| 196 | + |
171 | 197 | /** |
172 | 198 | * This should be accessed via {@link |
173 | 199 | * androidx.core.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)} |
@@ -748,6 +774,84 @@ public int getIconGravity() { |
748 | 774 | public void setIconGravity(@IconGravity int iconGravity) { |
749 | 775 | this.iconGravity = iconGravity; |
750 | 776 | } |
| 777 | + |
| 778 | + @Override |
| 779 | + protected int[] onCreateDrawableState(int extraSpace) { |
| 780 | + final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 781 | + if (isChecked()) { |
| 782 | + mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 783 | + } |
| 784 | + |
| 785 | + return drawableState; |
| 786 | + } |
| 787 | + |
| 788 | + /** |
| 789 | + * Register a callback to be invoked when the checked state of this MaterialButton changes. |
| 790 | + * |
| 791 | + * @param listener the callback to call on checked state change |
| 792 | + */ |
| 793 | + public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) { |
| 794 | + onCheckedChangeListener = listener; |
| 795 | + } |
| 796 | + |
| 797 | + @Override |
| 798 | + public void setChecked(boolean checked) { |
| 799 | + if (isCheckable() && isEnabled() && this.checked != checked) { |
| 800 | + this.checked = checked; |
| 801 | + refreshDrawableState(); |
| 802 | + |
| 803 | + // Avoid infinite recursions if setChecked() is called from a listener |
| 804 | + if (broadcasting) { |
| 805 | + return; |
| 806 | + } |
| 807 | + |
| 808 | + broadcasting = true; |
| 809 | + if (onCheckedChangeListener != null) { |
| 810 | + onCheckedChangeListener.onCheckedChanged(this, this.checked); |
| 811 | + } |
| 812 | + |
| 813 | + broadcasting = false; |
| 814 | + } |
| 815 | + } |
| 816 | + |
| 817 | + @Override |
| 818 | + public boolean isChecked() { |
| 819 | + return checked; |
| 820 | + } |
| 821 | + |
| 822 | + @Override |
| 823 | + public void toggle() { |
| 824 | + setChecked(!checked); |
| 825 | + } |
| 826 | + |
| 827 | + @Override |
| 828 | + public boolean performClick() { |
| 829 | + toggle(); |
| 830 | + |
| 831 | + return super.performClick(); |
| 832 | + } |
| 833 | + |
| 834 | + /** |
| 835 | + * Returns whether this MaterialButton is checkable. |
| 836 | + * |
| 837 | + * @see #setCheckable(boolean) |
| 838 | + * @attr ref com.google.android.material.R.styleable#MaterialButton_android_checkable |
| 839 | + */ |
| 840 | + public boolean isCheckable() { |
| 841 | + return materialButtonHelper != null && materialButtonHelper.isCheckable(); |
| 842 | + } |
| 843 | + |
| 844 | + /** |
| 845 | + * Sets whether this MaterialButton is checkable. |
| 846 | + * |
| 847 | + * @param checkable Whether this button is checkable. |
| 848 | + * @attr ref com.google.android.material.R.styleable#MaterialButton_android_checkable |
| 849 | + */ |
| 850 | + public void setCheckable(boolean checkable) { |
| 851 | + if (isUsingOriginalBackground()) { |
| 852 | + materialButtonHelper.setCheckable(checkable); |
| 853 | + } |
| 854 | + } |
751 | 855 |
|
752 | 856 | private boolean isUsingOriginalBackground() { |
753 | 857 | return materialButtonHelper != null && !materialButtonHelper.isBackgroundOverwritten(); |
|
0 commit comments