Combine 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

Load an ad

To make a combined native and banner request, do the following:

  1. Pass both NativeAdType.NATIVE type and NativeAdType.BANNER type as a list in the NativeAdRequest .

  2. Set at least one banner ad size.

The following examples loads a combined native and banner ad:

Kotlin

  val 
  
 adRequest 
  
 = 
  
 NativeAdRequest 
 . 
 Builder 
 ( 
 AD_UNIT_ID 
 , 
  
 listOf 
 ( 
 NativeAdType 
 . 
 NATIVE 
 , 
  
 NativeAdType 
 . 
 BANNER 
 )) 
  
 // Use setAdSize() or setAdSizes() depending on if you want multiple ad sizes or not. 
  
 . 
 setAdSizes 
 ( 
 listOf 
 ( 
 AdSize 
 . 
 BANNER 
 , 
  
 AdSize 
 . 
 LARGE_BANNER 
 )) 
  
 . 
 build 
 () 
 // Load the native and banner ad with the ad request and callback. 
 NativeAdLoader 
 . 
 load 
 ( 
 adRequest 
 , 
  
 getNativeAdLoaderCallback 
 ()) 
 

Java

  NativeAdRequest 
  
 adRequest 
  
 = 
  
 new 
  
 NativeAdRequest 
 . 
 Builder 
 ( 
 AD_UNIT_ID 
 , 
  
 List 
 . 
 of 
 ( 
 NativeAdType 
 . 
 NATIVE 
 , 
  
 NativeAdType 
 . 
 BANNER 
 )) 
  
 // Use setAdSize() or setAdSizes() depending on if you want multiple ad sizes or not. 
  
 . 
 setAdSizes 
 ( 
 Arrays 
 . 
 asList 
 ( 
 AdSize 
 . 
 BANNER 
 , 
  
 AdSize 
 . 
 LARGE_BANNER 
 )) 
  
 . 
 build 
 (); 
 // Load the native and banner ad with the ad request and callback. 
 NativeAdLoader 
 . 
 load 
 ( 
 adRequest 
 , 
  
 getNativeAdLoaderCallback 
 ()); 
 

Get the ad from the NativeAdLoaderCallback object

Depending on which type of ad has successfully loaded, the NativeAdLoaderCallback object calls the onNativeAdLoaded() method for native ads and the onBannerAdLoaded() method for banner ads.

The following example gets banner or native ads:

Kotlin

  private 
  
 fun 
  
 getNativeAdLoaderCallback 
 (): 
  
 NativeAdLoaderCallback 
  
 { 
  
 return 
  
 object 
  
 : 
  
 NativeAdLoaderCallback 
  
 { 
  
 override 
  
 fun 
  
 onNativeAdLoaded 
 ( 
 nativeAd 
 : 
  
 NativeAd 
 ) 
  
 { 
  
 // Called when a native ad has loaded. 
  
 } 
  
 override 
  
 fun 
  
 onBannerAdLoaded 
 ( 
 bannerAd 
 : 
  
 BannerAd 
 ) 
  
 { 
  
 // Called when a banner ad has loaded. 
  
 } 
  
 } 
 } 
 

Java

  private 
  
 NativeAdLoaderCallback 
  
 getNativeAdLoaderCallback 
 () 
  
 { 
  
 return 
  
 new 
  
 NativeAdLoaderCallback 
 () 
  
 { 
  
 @Override 
  
 public 
  
 void 
  
 onNativeAdLoaded 
 ( 
 @NonNull 
  
 NativeAd 
  
 nativeAd 
 ) 
  
 { 
  
 // Called when a native ad has loaded. 
  
 } 
  
 @Override 
  
 public 
  
 void 
  
 onBannerAdLoaded 
 ( 
 @NonNull 
  
 BannerAd 
  
 bannerAd 
 ) 
  
 { 
  
 // Called when a banner ad has loaded. 
  
 } 
  
 }; 
 } 
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: