Combining Custom-Rendered Native Ad and Banner Ad Requests

With a few changes to your code, you can combine native and banner ads in your ad requests.

Prerequisites

  • Version 11.0.0 or higher of the Google Mobile Ads SDK.
  • Complete Get Started.

Loading the ad

Custom-rendered native ads are loaded using the AdLoader class, which has its own AdLoader.Builder class to customize it during creation. By adding listeners to the AdLoader while building it, an app specifies which types of ad formats it is ready to receive. The AdLoader then requests just those types.

The AdLoader object can also be configured to make ad requests that can result in either a banner ad or a native ad. Adding an OnAdManagerAdViewLoadedListener to the AdLoader while building it specifies that banner ads should compete with native ads to fill the request.

The following code demonstrates how to build an AdLoader that can load either a native or banner ad in a single request:

Java

AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/nativeandbanner")
    .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
      @Override
      public void onNativeAdLoaded(NativeAd nativeAd) {
        // Show the ad.
      }
    })
    .forAdManagerAdView(new OnAdManagerAdViewLoadedListener() {
      @Override
      public void onAdManagerAdViewLoaded(AdManagerAdView adView) {
        // Show the banner ad.
      }
    }, AdSize.BANNER, AdSize.MEDIUM_RECTANGLE)
    .withAdListener(new AdListener() {
      @Override
      public void onAdFailedToLoad(LoadAdError error) {
        // Handle the failure by logging, altering the UI, and so on.
      }
    })
    .withAdManagerAdViewOptions(new AdManagerAdViewOptions.Builder()
      // Methods in the AdManagerAdViewOptions.Builder class can be
      // used here to specify individual options settings.
      .build())
    .build();

Kotlin

val adLoader = AdLoader.Builder(this, "/6499/example/nativeandbanner")
    .forNativeAd { nativeAd ->
      // Show the ad.
    }
    .forAdManagerAdView({ adView ->
      // Show the banner ad.
    }, AdSize.BANNER, AdSize.MEDIUM_RECTANGLE)
    .withAdListener(object: AdListener() {
      override fun onAdFailedToLoad(adError: LoadAdError) {
        // Handle the failure by logging, altering the UI, and so on.
      }
    })
    .withAdManagerAdViewOptions(AdManagerAdViewOptions.Builder()
      // Methods in the AdManagerAdViewOptions.Builder class can be
      // used here to specify individual options settings.
      .build())
    .build()

The forAdManagerAdView() method above prepares the AdLoader to receive banner ads. A variable-length list of valid ad sizes must be specified alongside an OnAdManagerAdViewLoadedListener when invoking forAdManagerAdView().

To make a valid ad request, at least one valid ad size must be specified. When a banner ad has loaded successfully, the specified listener object's onAdManagerAdViewLoaded() method is called.

Setting AdViewOptions

The last function included in the creation of the AdLoader above is another optional method, withAdManagerAdViewOptions():

Java

.withAdManagerAdViewOptions(new AdManagerAdViewOptions.Builder()
    // Methods in the AdManagerAdViewOptions.Builder class can be
    // used here to specify individual banner options settings.
    .build()

Kotlin

.withAdManagerAdViewOptions(AdManagerAdViewOptions.Builder()
    // Methods in the AdManagerAdViewOptions.Builder class can be
    // used here to specify individual banner options settings.
    .build()

The AdManagerAdViewOptions object lets publishers set specific options for banners loaded by the AdLoader, such as:

setManualImpressionsEnabled()
Enables manual impression reporting for Google Ad Manager reservations. Apps using manual impressions can determine for themselves when an impression should be recorded, and can do so by calling AdManagerAdView.recordManualImpression().