|
21 | 21 | import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap; |
22 | 22 |
|
23 | 23 | import android.content.Context; |
24 | | -import android.os.Build.VERSION; |
25 | | -import android.os.Build.VERSION_CODES; |
26 | 24 | import android.util.AttributeSet; |
| 25 | +import android.view.SoundEffectConstants; |
27 | 26 | import android.view.View; |
28 | 27 | import android.view.ViewGroup; |
29 | 28 | import android.widget.Button; |
30 | 29 | import androidx.annotation.NonNull; |
31 | 30 | import androidx.annotation.Nullable; |
| 31 | +import androidx.core.view.ViewCompat; |
32 | 32 |
|
33 | 33 | /** |
34 | 34 | * A container for two {@link MaterialButton}s that together create a split button. The {@link |
@@ -108,24 +108,28 @@ public void addView(@NonNull View child, int index, @Nullable ViewGroup.LayoutPa |
108 | 108 | if (indexOfChild(child) == 1) { |
109 | 109 | buttonChild.setCheckable(true); |
110 | 110 | buttonChild.setA11yClassName(Button.class.getName()); |
111 | | - if (VERSION.SDK_INT >= VERSION_CODES.R) { |
112 | | - // Set initial content description based on checked state when focused. |
113 | | - buttonChild.setStateDescription( |
114 | | - getResources() |
115 | | - .getString( |
116 | | - buttonChild.isChecked() |
117 | | - ? R.string.mtrl_button_expanded_content_description |
118 | | - : R.string.mtrl_button_collapsed_content_description)); |
| 111 | + // Set initial content description based on checked state when focused. |
| 112 | + ViewCompat.setStateDescription( |
| 113 | + buttonChild, |
| 114 | + getResources() |
| 115 | + .getString( |
| 116 | + buttonChild.isChecked() |
| 117 | + ? R.string.mtrl_button_expanded_content_description |
| 118 | + : R.string.mtrl_button_collapsed_content_description)); |
119 | 119 |
|
120 | | - buttonChild.addOnCheckedChangeListener( |
121 | | - (button, isChecked) -> |
122 | | - button.setStateDescription( |
123 | | - getResources() |
124 | | - .getString( |
125 | | - isChecked |
126 | | - ? R.string.mtrl_button_expanded_content_description |
127 | | - : R.string.mtrl_button_collapsed_content_description))); |
128 | | - } |
| 120 | + buttonChild.addOnCheckedChangeListener( |
| 121 | + (button, isChecked) -> { |
| 122 | + // Play sound effect when checked state changes. |
| 123 | + button.playSoundEffect(SoundEffectConstants.CLICK); |
| 124 | + // Update content description when checked state changes. |
| 125 | + ViewCompat.setStateDescription( |
| 126 | + buttonChild, |
| 127 | + getResources() |
| 128 | + .getString( |
| 129 | + isChecked |
| 130 | + ? R.string.mtrl_button_expanded_content_description |
| 131 | + : R.string.mtrl_button_collapsed_content_description)); |
| 132 | + }); |
129 | 133 | } |
130 | 134 | } |
131 | 135 | } |
0 commit comments