Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob doesn't show ad until second request

There are a few questions out there about this and a few different answers - none of which seem to work for me.

I have a libdgx app, which complicates things slightly I guess, but I don't see that it should be causing the problem. Code for my onCreate is below.

I have an AdListener assigned that is called when the add is loaded. This code is being hit but nothing is being shown. If I set the advert to refresh then it will show after the first refresh. If I call loadAd a second time after a brief wait the banner will show. If I call loadAd on two consecutive lines of code the advert will not show. Any pointers as to what might be going on here would be appreciated.

edit: I also just noticed that I can click the advert, even though I can't see the advert. When I return back to the game having clicked the advert the advert displays correctly.

edit2: I changed the advert to a SMART_BANNER instead of a BANNER and that seems to work as expected. I still have no idea why the BANNER isn't working.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create the layout
    FrameLayout layout = new FrameLayout(this);


    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    );
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.useAccelerometer = false;
    config.useCompass = false;
    config.useWakelock = false;
    config.useImmersiveMode = true;
    config.hideStatusBar = true;

    // Create the libgdx View
    View gameView = initializeForView(new WordStormGame(this), config);

    // Add the libgdx view
    layout.addView(gameView);

    adView = new AdView(this);
    adView.setAdUnitId("xxxxxxxxxxxx");
    adView.setAdSize(AdSize.BANNER);
    adView.setAdListener(new AdListener() {
                             public void onAdLoaded()
                                 Gdx.app.log("Advert", "Ad Loaded");
                             }
                         }
    );

    // Add the AdMob view
    FrameLayout.LayoutParams adsParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.TOP | android.view.Gravity.CENTER_HORIZONTAL);
    layout.addView(adView, adsParams);

    // Hook it all up
    setContentView(layout);

    adView.loadAd(new AdRequest.Builder().build());
}
like image 644
Will Calderwood Avatar asked Dec 17 '25 15:12

Will Calderwood


1 Answers

Solving this was eventually as simple as adding the line

adView.setBackgroundColor(Color.TRANSPARENT);

My final adview creation code looks like this:

private void createAdView() {
    abBuilder = new AdRequest.Builder();
    abBuilder.addTestDevice("xxxxxxx");

    if (adView != null)
        ((ViewManager) adView.getParent()).removeView(adView);

    adView = new AdView(this);
    adView.setAdUnitId("xxxxxxx");
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdListener(new AdListener() {
                             public void onAdLoaded() {
                                 if (adsShown)
                                     adView.setVisibility(View.VISIBLE);
                                 Gdx.app.log("Advert", "Ad Loaded");
                             }
                         }
    );

    adView.setBackgroundColor(Color.TRANSPARENT);

    // Add the AdMob view
    RelativeLayout.LayoutParams adParams =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    layout.addView(adView, adParams);
}
like image 162
Will Calderwood Avatar answered Dec 19 '25 06:12

Will Calderwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!