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
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"
/>
To load a banner ad:
- Create an
AdViewobject with ad unit ID and size. - Add the
AdViewto view hierarchy. - 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 ); } }
To load a banner ad:
- Create an
AdViewobject. - Load the ad.
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 ( " AD_UNIT_ID " , 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
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 ( " AD_UNIT_ID " , 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 KotlinadView . adListener = object : AdListener () { override fun onAdLoaded () { // Called when an ad has loaded. } override fun onAdFailedToLoad ( adError : LoadAdError ) { // Called when ad fails to load. } } JavaadView . 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. KotlinadView . loadAd ( 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 ( adError : LoadAdError ) { // Called when the ad fails to refresh. } } } override fun onAdFailedToLoad ( adError : LoadAdError ) { // Called when ad fails to load. } } ) JavaadView . loadAd ( 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. } }); } @Override public void onAdFailedToLoad ( @NonNull LoadAdError adError ) { // Called when ad fails to load. } }); |

