AI-generated Key Takeaways
-
Combine native and banner ads in your ad requests with a few code changes.
-
You need Google Mobile Ads SDK version 11.0.0 or higher and to complete the Get started guide.
-
Use the
AdLoaderclass and itsAdLoader.Builderto load custom-rendered native ads. -
Adding an
OnAdManagerAdViewLoadedListenerto theAdLoaderallows banner ads to compete with native ads for the request. -
You can use
withAdManagerAdViewOptions()to set specific options for banners loaded by theAdLoader.
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.
Load an 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, "/21775744923/example/native-and-banner") .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, etc. } }) .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, "/21775744923/example/native-and-banner") .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, etc. } }) .withAdManagerAdViewOptions(AdManagerAdViewOptions.Builder() // Methods in the AdManagerAdViewOptions.Builder class can be // used here to specify individual options settings. .build()) .build()
The forAdManagerAdView() method 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.
Set the AdViewOptions object
The last function included in the creation of the AdLoader 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().