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 the Get Started guide

Loading the ad

Custom-rendered native ads are loaded via 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:

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();

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():

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

The AdManagerAdViewOptions object allows publishers to 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().