Skip to content

Commit 1ee0768

Browse files
paulfthomashunterstich
authored andcommitted
[BottomSheet] Make handle respond to keyboard input
PiperOrigin-RevId: 775207544
1 parent 1bc9d1e commit 1ee0768

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

lib/java/com/google/android/material/bottomsheet/BottomSheetDragHandleView.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.view.GestureDetector;
3232
import android.view.GestureDetector.OnGestureListener;
3333
import android.view.GestureDetector.SimpleOnGestureListener;
34+
import android.view.KeyEvent;
3435
import android.view.MotionEvent;
3536
import android.view.View;
3637
import android.view.ViewGroup.LayoutParams;
@@ -366,4 +367,24 @@ private static View getParentView(View view) {
366367
ViewParent parent = view.getParent();
367368
return parent instanceof View ? (View) parent : null;
368369
}
370+
371+
@Override
372+
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
373+
if (!isEnabled()) {
374+
return super.onKeyDown(keyCode, event);
375+
}
376+
377+
switch (keyCode) {
378+
case KeyEvent.KEYCODE_DPAD_CENTER:
379+
case KeyEvent.KEYCODE_ENTER:
380+
if (hasClickListener) {
381+
return performClick();
382+
}
383+
return expandOrCollapseBottomSheetIfPossible();
384+
default:
385+
// Nothing to do in this case.
386+
}
387+
388+
return super.onKeyDown(keyCode, event);
389+
}
369390
}

lib/javatests/com/google/android/material/bottomsheet/BottomSheetDragHandleTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.os.Bundle;
2828
import androidx.appcompat.app.AppCompatActivity;
2929
import android.util.SparseIntArray;
30+
import android.view.KeyEvent;
3031
import android.view.View;
3132
import android.view.ViewGroup.LayoutParams;
3233
import android.view.accessibility.AccessibilityManager;
@@ -109,6 +110,25 @@ public void test_customClickListenerOverridesInternalClickBehavior() {
109110
.isEqualTo(BottomSheetBehavior.STATE_EXPANDED);
110111
}
111112

113+
@Test
114+
public void test_customClickListenerOverridesInternalClickBehaviorWithKeyboardEnter() {
115+
activity.addViewToBottomSheet(dragHandleView);
116+
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
117+
shadowOf(accessibilityManager).setEnabled(true);
118+
dragHandleView.setOnClickListener(
119+
v -> {
120+
// do nothing
121+
});
122+
123+
boolean unused =
124+
dragHandleView.onKeyDown(
125+
KeyEvent.KEYCODE_ENTER, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
126+
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
127+
128+
assertThat(activity.bottomSheetBehavior.getState())
129+
.isEqualTo(BottomSheetBehavior.STATE_EXPANDED);
130+
}
131+
112132
@Test
113133
public void test_notInteractableWhenNotAttachedToBottomSheetAndAccessibilityEnabled() {
114134
activity.addViewToContainer(dragHandleView);
@@ -152,6 +172,39 @@ public void test_collapseExpandedBottomSheetWhenClicked() {
152172
.isEqualTo(BottomSheetBehavior.STATE_COLLAPSED);
153173
}
154174

175+
@Test
176+
public void test_expandCollapsedBottomSheetWithKeyboardEnter() {
177+
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
178+
activity.addViewToBottomSheet(dragHandleView);
179+
shadowOf(accessibilityManager).setEnabled(true);
180+
boolean unused =
181+
dragHandleView.onKeyDown(
182+
KeyEvent.KEYCODE_ENTER, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
183+
184+
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
185+
186+
assertThat(activity.bottomSheetBehavior.getState())
187+
.isEqualTo(BottomSheetBehavior.STATE_EXPANDED);
188+
}
189+
190+
@Test
191+
public void test_collapseExpandedBottomSheetWithKeyboardEnter() {
192+
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
193+
activity.addViewToBottomSheet(dragHandleView);
194+
shadowOf(accessibilityManager).setEnabled(true);
195+
196+
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
197+
198+
boolean unused =
199+
dragHandleView.onKeyDown(
200+
KeyEvent.KEYCODE_ENTER, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
201+
202+
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
203+
204+
assertThat(activity.bottomSheetBehavior.getState())
205+
.isEqualTo(BottomSheetBehavior.STATE_COLLAPSED);
206+
}
207+
155208
@Test
156209
public void test_collapsedBottomSheetMoveToHalfExpanded_whenClickedAndFitToContentsFalse() {
157210
activity.bottomSheetBehavior.setFitToContents(false);

0 commit comments

Comments
 (0)