Admob interstitial ad won't display

user2661663 picture user2661663 · Dec 31, 2013 · Viewed 40k times · Source

I used to display AdMob banner on my future apps, and I'd like to give a try to the interstitial ads.

I checked the AdMob SDK for implementation, and I copied their example source because it was exactly what I want (i.e. the interstitial shown when the activity launch).

I tried it on emulator and on my Galaxy, no ad has been displayed.

Here is the source code:

public class Asscreed extends Activity {
    private InterstitialAd interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asscreed);

        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-6046034785851961/xxxxxx");

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);
    }

    // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

The imports are OK and the Google Play Services library is of course imported.

I use this example: AdMob Android Guides - Interstitial Ad.

Could someone tell me what's wrong in my code?

Answer

Kumar Bibek picture Kumar Bibek · Dec 31, 2013

You should wait for the ad to be loaded. Only then, you can call displayInterstial() method, which would show the ad.

You can register for a listener, that will let you know when the loading is done.

interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
});