Question

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...

Was it helpful?

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;
    }
}

OTHER TIPS

This works for me:

    `// ADS
    MobileAds.initialize(getApplicationContext(),  getResources().getString(R.string.banner_ad_unit_id));
    mAdView = (AdView) findViewById(R.id.adView);
    adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            RelativeLayout fl = (RelativeLayout) findViewById(R.id.frameCounter);
            fl.setFocusableInTouchMode(true);
            fl.requestFocus();
        }
    });`

on the onAdLoaded event, I've set the focus to my layout and voile!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top