Will Calderwood
Will Calderwood

Reputation: 4636

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

Upvotes: 3

Views: 1005

Answers (3)

Will Calderwood
Will Calderwood

Reputation: 4636

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

Upvotes: 5

Cord Rehn
Cord Rehn

Reputation: 1149

I had the same exact issue with smart banner and I fixed it by adding the view to the layout thru the AdListener once an ad was loaded:

        // Create and setup the AdMob view
        AdRequest.Builder ad_builder = new AdRequest.Builder();
        ad_builder.addTestDevice("XXXXXXXXXXXXX"); // my HTC phone
        m_AdRequest = ad_builder.build();

        m_AdView = new AdView(this);
        m_AdView.setAdSize(AdSize.SMART_BANNER);
        m_AdView.setAdUnitId("ZZZZZZZZZZZZ");
        m_AdView.setAdListener(new AdListener() {
            private boolean _first = true;

            @Override
            public void onAdLoaded() {
                if (_first) {
                    _first = false;

                    // Add the AdMob view when the first ad gets loaded, this makes sure the first ad gets displayed
                    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); // places the ad at the bottom overlapping your game

                    layout.addView(m_AdView, adParams);
                }
            }
        });

Hope this solves your headache!

Upvotes: 0

furkick
furkick

Reputation: 423

Just a thought, have you included this code? It is required by all ads.

public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

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

    /** Called before the activity is destroyed. */
    @Override
    public void onDestroy() {
        // Destroy the AdView.
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

Upvotes: 0

Related Questions