Adding Interstitial Ads to your Android app

The Audience Network allows you to monetize your Android apps with Facebook ads. An interstitial ad is a full screen ad that you can show in your app. Typically interstitial ads are shown when there is a transition in your app. For example -- after finishing a level in a game or after loading a story in a news app.

Ensure you have completed the Audience Network Getting Started and Android Getting Started guides before you proceed.

Ad Banner Steps

Step 1: Implementing Interstitial Ads in your Activity

Step 2: Showing Interstitial Ads in your Activity

Step 1: Initializing Interstitial Ads in your Activity

Add the following code at the top of your Activity in order to import the Facebook Ads SDK:

import com.facebook.ads.*;

Initialize the InterstitialAd.

private InterstitialAd interstitialAd;

@Override
public void onCreate(Bundle savedInstanceState) {
...
  // Instantiate an InterstitialAd object
  interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
...  

Step 2: Showing Interstitial Ads

Set an InterstitialAdListener and load the Ad.

import android.widget.Toast;
...

  public void onCreate(Bundle savedInstanceState) {
   ...
    // Set listeners for the Interstitial Ad
    interstitialAd.setAdListener(new InterstitialAdListener() {
      @Override
      public void onInterstitialDisplayed(Ad ad) {
        // Interstitial displayed callback
      }

      @Override
      public void onInterstitialDismissed(Ad ad) {
        // Interstitial dismissed callback
      }

      @Override
      public void onError(Ad ad, AdError adError) {
        // Ad error callback
        Toast.makeText(MainActivity.this, "Error: " + adError.getErrorMessage(),
        Toast.LENGTH_LONG).show();
      }

      @Override
      public void onAdLoaded(Ad ad) {
        // Show the ad when it's done loading.
        interstitialAd.show();                
      }

      @Override
      public void onAdClicked(Ad ad) {
        // Ad clicked callback
      }
    });

    // Load the interstitial ad
    interstitialAd.loadAd();
  }

Interstitial Ads have creatives that are larger in size so a good practice is calling loadAd() in advance and then calling show() at the appropriate time.

Finally, add the following code to your Activity's onDestroy() function to release resources the InterstitialAd uses:

@Override
protected void onDestroy() {
  if (interstitialAd != null) {
    interstitialAd.destroy();
  }
  super.onDestroy();
}

If you are using the default Google Android emulator, you'll add the following line of code before loading a test ad:
AdSettings.addTestDevice("HASHED ID");.

Use the hashed ID that is printed to logcat when you first make a request to load an ad on a device.

Genymotion and physical devices do not need this step. If you would like to test with real ads, please consult our Testing Guide.

Start your app and you should see an Interstitial Ad appear:

Next Steps