Search code examples
androidlibgdx

Libgdx -> AdMob my banner is visible after clicked


I configured admob in my application, but the problem is when i start a game. I just not see a banner, just when i click it then add is opening and when i back to game i see the banner. I don't know whats wrong. My application have a black background

Here is my code:

adView = new AdView(this);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = true;
RelativeLayout relativeLayout = new RelativeLayout(this);
View gameView = initializeForView(new SkippyFlowersGame(this), cfg);
relativeLayout.addView(gameView);
adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Log.i(TAG, "Ad Loaded...");
        }
    });
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("XXXXXXXXXXXXXXXXXXXXXXXXXXX");
AdRequest.Builder builder = new AdRequest.Builder();
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.addView(adView, adParams);
adView.loadAd(builder.build());
setContentView(relativeLayout);

Solution

  • Make sure you're calling pause(), resume() and destroy() methods of AdView by overriding methods of Activity in this way :

    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }
    
    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }
    
    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }
    

    Moreover still having problem, take a look of this answer. It may be helpful.