AI-generated Key Takeaways
-
Combine native and banner ads in your ad requests by making a few code changes.
-
To make a combined request, pass both
NativeAdType.NATIVE
andNativeAdType.BANNER
types as a list in theNativeAdRequest
and set at least one banner ad size. -
You can only request banner ads through
NativeAdRequest
when you also request native ads; separate banner requests are handled differently and banners loaded this way do not refresh. -
The
NativeAdLoaderCallback
object calls specific methods depending on whether a native ad (onNativeAdLoaded()
) or a banner ad (onBannerAdLoaded()
) has successfully loaded.
With a few changes to your code, you can combine native and banner ads in your ad requests.
Prerequisites
- Complete the Get started guide.
Load an ad
To make a combined native and banner request, do the following:
Pass both
NativeAdType.NATIVE
type andNativeAdType.BANNER
type as a list in theNativeAdRequest
.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.
}
};
}