Search code examples
androidadmobjoystickadviewgamepad

Android: AdMob view disrupts input from gamepad (onKey events stop working)


I’m displaying Admob banner ads in my game and I noticed that the AdView somehow messes up with the input from the gamepad.
For example buttons like X, A, L1, R1 stop working, they don't trigger onKeyDown and onKeyUp events. And other buttons generate incorrect key codes, for example when Y is pressed, instead of KEYCODE_BUTTON_Y I receive KEYCODE_BUTTON_MENU.

This problem happens after I touch the screen (not the banner itself). If I don't touch the screen, the gamepad works fine.

So it seems like AdView is 'stealing' key events, or something like that.

I experimented with different AdView settings like setFocusable(false), position, tried assigning it custom KeyListener – nothing helps.
I tried 2 different gamepads and different android devices.
This problem disappears only when I remove the AdView or make it invisible...


Solution

  • Ok, seems like I found the solution. If I override dispatchKeyEvent and cancel dispatching onKey events to the AdView, then the gamepad works fine. Not sure if it's OK to do this, but it works.

    public class MyAdView extends AdView {
        public MyAdView(Activity activity, AdSize adSize, String adUnitId) {
            super(activity, adSize, adUnitId);
        }
    
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            return false;
        }
    }