0

I use ViewPager for a wizard flow in the application and I want to disable page switches from any user input (How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?).

Recently I discovered that when I run the application in an emulator I can still swipe page with PC keyboard left and right arrow keys.

Can I disable it? Is it a real event that can happen on a real device?

Community
  • 1
  • 1
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114

1 Answers1

1

Well, I can't reproduce your issue in a ViewPager based project running on the emulator, but since your already extending the ViewPager, you can try overriding the method below to ignore the right and left keypresses.

Untested:

 @Override
   public boolean onKeyPreIme(int keyCode, KeyEvent event) {

      switch( keyCode ) {
         case KeyEvent.KEYCODE_DPAD_RIGHT:
         case KeyEvent.KEYCODE_DPAD_LEFT:
            return true;
      }
      return false;
   }

I would imagine that this is possible on hardware if the user has a physical keyboard connected.

Update

Try this in your extended class:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    switch( event.getAction() ) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
        case KeyEvent.KEYCODE_DPAD_LEFT:
            return true;
    }
    return super.dispatchKeyEvent(event);
}
Gary Bak
  • 4,746
  • 4
  • 22
  • 39
  • Try a adding a breakpoint in ViewPager.executeKeyEvent before the arrowScroll() calls. If it breaks there, check the call stack to see where is the best spot to override the event. – Gary Bak Oct 11 '16 at 21:13
  • Thanks! It was exactly `executeKeyEvent` with `DPAD_*` – Eugen Martynov Oct 12 '16 at 11:25