This page covers the differences in loading and showing a banner ad
between the current SDK and GMA Next-Gen SDK.

Given a view hierarchy with an [`AdView`](https://developers.google.com/admob/android/next-gen/reference/com/google/android/libraries/ads/mobile/sdk/banner/AdView) defined:  

    <com.google.android.libraries.ads.mobile.sdk.banner.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

|---|---|
| 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 ```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 = "<var class="readonly" translate="no">AD_UNIT_ID</var>" 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 ```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("<var class="readonly" translate="no">AD_UNIT_ID</var>"); 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); } } ``` |
| GMA Next-Gen SDK | To load a banner ad: 1. Create an `AdView` object. 2. Load the ad. ### Kotlin ```kotlin import android.util.Log import com.google.android.libraries.ads.mobile.sdk.banner.AdSize import com.google.android.libraries.ads.mobile.sdk.banner.AdView 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.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 val TAG = "MainActivity" private lateinit var adView: AdView private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) // Step 1 - Create an AdView object. adView = binding.adView // Step 2 - Load the ad. val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360) val adRequest = BannerAdRequest.Builder("<var class="readonly" translate="no">AD_UNIT_ID</var>", adSize).build() adView.loadAd( adRequest, object : AdLoadCallback<BannerAd> { override fun onAdLoaded(ad: BannerAd) { ad.adEventCallback = object : BannerAdEventCallback { override fun onAdImpression() { Log.d(TAG, "Banner ad recorded an impression.") } override fun onAdClicked() { Log.d(TAG, "Banner ad clicked.") } } } override fun onAdFailedToLoad(adError: LoadAdError) { Log.e(TAG, "Banner ad failed to load: $adError") } }, ) } } ``` ### Java ```java import android.util.Log; import com.google.android.libraries.ads.mobile.sdk.banner.AdSize; import com.google.android.libraries.ads.mobile.sdk.banner.AdView; 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.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 static final String TAG = "MainActivity"; private AdView adView; private ActivityMainBinding binding; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // Step 1 - Create an AdView object. adView = binding.adView; // Step 2 - Load the ad. AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360); BannerAdRequest adRequest = new BannerAdRequest.Builder("<var class="readonly" translate="no">AD_UNIT_ID</var>", adSize).build(); adView.loadAd( adRequest, new AdLoadCallback<BannerAd>() { @Override public void onAdLoaded(@NonNull BannerAd ad) { ad.setAdEventCallback( new BannerAdEventCallback() { @Override public void onAdImpression() { Log.d(TAG, "Banner ad recorded an impression."); } @Override public void onAdClicked() { Log.d(TAG, "Banner ad clicked."); } }); } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { Log.e(TAG, "Banner ad failed to load: " + adError); } }); } } ``` |

### Review banner refresh callbacks

GMA Next-Gen 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 ```kotlin adView.adListener = object : AdListener() { override fun onAdLoaded() { // Called when an ad has loaded. } override fun onAdFailedToLoad(adError : LoadAdError) { // Called when ad fails to load. } } ``` ### Java ```java adView.setAdListener( new AdListener() { @Override public void onAdLoaded() { // Called when an ad has loaded. } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { // Called when ad fails to load. } }); ``` |
| GMA Next-Gen SDK | Set the refresh callback on the loaded banner ad to listen for ad refresh events. ### Kotlin ```kotlin adView.loadAd( BannerAdRequest.Builder("<var class="readonly" translate="no">AD_UNIT_ID</var>", 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(adError: LoadAdError) { // Called when the ad fails to refresh. } } } override fun onAdFailedToLoad(adError: LoadAdError) { // Called when ad fails to load. } } ) ``` ### Java ```java adView.loadAd( new BannerAdRequest.Builder("<var class="readonly" translate="no">AD_UNIT_ID</var>", 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. } }); } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { // Called when ad fails to load. } }); ``` |