Migrate banner ads

This page covers the differences in loading and showing a banner ad between the current and Next Gen Mobile Ads SDKs.

The Next Gen Mobile Ads SDK introduces a clear separation between the banner ad object (BannerAd) and its View. The major impact of this change is the banner ad view is no longer available until the ad loads.

Given a view hierarchy with a banner view container defined:

<FrameLayout
    android:id="@+id/banner_view_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentBottom="true" />
Current

To load a banner ad:

  1. Create an AdView object with ad unit ID and size.
  2. Add the AdView to view hierarchy.
  3. Load the ad.

Kotlin

import com.google.android.gms.ads.AdView

class MainActivity : AppCompatActivity() {

  private lateinit var binding: ActivityMainBinding
  private lateinit var adView: AdView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Step 1 - Create an AdView object with ad unit ID and size.
    adView = AdView(this)
    adView.adUnitId = "AD_UNIT_ID"
    adView.setAdSize(
      AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 320)
    )
    // Step 2 - Add the AdView to view hierarchy.
    binding.bannerViewContainer.addView(adView)

    // Step 3 - Load the ad.
    val adRequest = AdRequest.Builder().build()
    adView.loadAd(adRequest)
  }
}

Java

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {

  private ActivityMainBinding binding;
  private AdView adView;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    // Step 1 - Create an AdView object with ad unit ID and size.
    adView = new AdView(this);
    adView.setAdUnitId("AD_UNIT_ID");
    adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 320));

    // Step 2 - Add the AdView to view hierarchy.
    binding.bannerViewContainer.addView(adView);

    // Step 3 - Load the ad.
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
  }
}
Next Gen

To load a banner ad:

  1. Create a BannerAdRequest object with ad unit ID and size.
  2. Load the ad.
  3. Call BannerAd.getView() to get the View and add it to view hierarchy.

Kotlin

import com.google.android.libraries.ads.mobile.sdk.banner.AdSize
import com.google.android.libraries.ads.mobile.sdk.banner.BannerAd
import com.google.android.libraries.ads.mobile.sdk.banner.BannerAdEventCallback
import com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRefreshCallback
import com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRequest
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError

class MainActivity : AppCompatActivity() {

  private var bannerAd: BannerAd? = null
  private lateinit var binding: ActivityMainBinding
  private lateinit var adSize: AdSize
  private lateinit var bannerViewContainer: FrameLayout

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Give the banner container a placeholder height to avoid a sudden layout
    // shifts when the ad loads.
    adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 320)
    bannerViewContainer = binding.bannerViewContainer
    val bannerLayoutParams = bannerViewContainer.layoutParams
    bannerLayoutParams.height = adSize.getHeightInPixels(requireContext())
    bannerViewContainer.layoutParams = bannerLayoutParams

    // Step 1 - Create a BannerAdRequest object with ad unit ID and size.
    val adRequest = BannerAdRequest.Builder("AD_UNIT_ID", adSize).build()

    // Step 2 - Load the ad.
    BannerAd.load(
      adRequest,
      object : AdLoadCallback<BannerAd> {
        override fun onAdLoaded(ad: BannerAd) {
          // Assign the loaded ad to the BannerAd object.
          bannerAd = ad
          // Step 3 - Call BannerAd.getView() to get the View and add it
          // to view hierarchy on the UI thread.
          activity?.runOnUiThread {
            binding.bannerViewContainer.addView(ad.getView(requireActivity()))
          }
        }

        override fun onAdFailedToLoad(loadAdError: LoadAdError) {
          bannerAd = null
        }
      },
    )
  }
}

Java

import com.google.android.libraries.ads.mobile.sdk.banner.AdSize;
import com.google.android.libraries.ads.mobile.sdk.banner.BannerAd;
import com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRefreshCallback;
import com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRequest;
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback;
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError;

public class MainActivity extends AppCompatActivity {

  private BannerAd bannerAd;
  private ActivityMainBinding binding;
  private AdSize adSize;
  private FrameLayout bannerViewContainer;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    // Give the banner container a placeholder height to avoid a sudden layout
    // shifts when the ad loads.
    adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 320);
    bannerViewContainer = binding.bannerViewContainer;
    LayoutParams bannerLayoutParams = bannerViewContainer.getLayoutParams();
    bannerLayoutParams.height = adSize.getHeightInPixels(this);
    bannerViewContainer.setLayoutParams(bannerLayoutParams);

    // Step 1 - Create a BannerAdRequest object with ad unit ID and size.
    BannerAdRequest adRequest = new BannerAdRequest.Builder("AD_UNIT_ID", adSize).build();

    // Step 2 - Load the ad.
    BannerAd.load(
        adRequest,
        new AdLoadCallback<BannerAd>() {
          @Override
          public void onAdLoaded(@NonNull BannerAd ad) {
            // Assign the loaded ad to the BannerAd object.
            bannerAd = ad;
            // Step 3 - Call BannerAd.getView() to get the View and add it
            // to view hierarchy on the UI thread.
            runOnUiThread(
                () -> binding.bannerViewContainer.addView(ad.getView(MainActivity.this)));
          }

          @Override
          public void onAdFailedToLoad(@NonNull LoadAdError adError) {
            bannerAd = null;
          }
        });
  }
}

The Next Gen Mobile Ads SDK includes a listener for automatic banner ad refreshes:

Current

The Mobile Ads SDK calls the onAdLoaded() and onAdFailedToLoad() callbacks to indicate success or failure of an ad refresh.

Kotlin

adView.adListener = object : AdListener() {
    override fun onAdLoaded() {
      // Called when an ad has loaded.
    }

    override fun onAdFailedToLoad(loadAdError : LoadAdError) {
      // Called when ad fails to load.
    }
}

Java

adView.setAdListener(
    new AdListener() {
      @Override
      public void onAdLoaded() {
        // Called when an ad has loaded.
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Called when ad fails to load.
      }
    });
Next Gen

Set the refresh callback on the loaded banner ad to listen for ad refresh events.

Kotlin

BannerAd.load(
  BannerAdRequest.Builder("AD_UNIT_ID", adSize).build(),
  object : AdLoadCallback<BannerAd> {
    override fun onAdLoaded(ad: BannerAd) {
      // Called when an ad has loaded.
      ad.adEventCallback =
        object : BannerAdEventCallback {
        }

      ad.bannerAdRefreshCallback =
        object : BannerAdRefreshCallback {
          // Set the ad refresh callbacks.
          override fun onAdRefreshed() {
            // Called when the ad refreshes.
          }

          override fun onAdFailedToRefresh(loadAdError: LoadAdError) {
            // Called when the ad fails to refresh.
          }
        }

      bannerAd = ad
    }

    override fun onAdFailedToLoad(loadAdError: LoadAdError) {
      // Called when ad fails to load.
    }
  }
)

Java

BannerAd.load(
    new BannerAdRequest.Builder("AD_UNIT_ID", adSize).build(),
    new AdLoadCallback<BannerAd>() {
      @Override
      public void onAdLoaded(@NonNull BannerAd ad) {
        // Called when an ad has loaded.
        ad.setAdEventCallback(new BannerAdEventCallback() {});

        ad.setBannerAdRefreshCallback(
            // Set the ad refresh callbacks.
            new BannerAdRefreshCallback() {
              @Override
              public void onAdRefreshed() {
                // Called when the ad refreshes.
              }

              @Override
              public void onAdFailedToRefresh(@NonNull LoadAdError adError) {
                // Called when the ad fails to refresh.
              }
            });

        bannerAd = ad;
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError adError) {
        // Called when ad fails to load.
      }
    });