Native ads allow you to make additional customizations using the
NativeAdOptions
object. This guide shows you how to use NativeAdOptions
.
Setting options
withNativeAdOptions()
- The last function included in the
creation of the
AdLoader
is another optional method,withNativeAdOptions()
:
Java
AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110") .forNativeAd(new NativeAd.OnNativeAdLoadedListener() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Show the ad. } }) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(LoadAdError adError) { // Handle the failure by logging, altering the UI, and so on. } }) .withNativeAdOptions(new NativeAdOptions.Builder() // Methods in the NativeAdOptions.Builder class can be // used here to specify individual options settings. .build()) .build();
Kotlin
val adLoader = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") .forNativeAd { ad : NativeAd -> // Show the ad. } .withAdListener(object : AdListener() { override fun onAdFailedToLoad(adError: LoadAdError) { // Handle the failure by logging, altering the UI, and so on. } }) .withNativeAdOptions(NativeAdOptions.Builder() // Methods in the NativeAdOptions.Builder class can be // used here to specify individual options settings. .build()) .build()
The NativeAdOptions
object allows apps to set specific options used in making the request. Its
Builder
class offers the following methods to use when creating an instance.
setReturnUrlsForImageAssets()
- Image assets for native ads are returned via instances of
NativeAd.Image
, which holds aDrawable
and aUri
. If this option is set to false (which is the default), the Google Mobile Ads SDK fetches image assets automatically and populates both theDrawable
and theUri
for you. If it's set to true, however, the SDK instead populates just theUri
field, allowing you to download the actual images at your discretion. setRequestMultipleImages()
Some image assets contain a series of images rather than just one. By setting this value to true, your app indicates that it's prepared to display all the images for any assets that have more than one. By setting it to false (default) your app instructs the SDK to provide just the first image for any assets that contain a series.
If
withNativeAdOptions
is not called at all when creating anAdLoader
, the default value for each option is used.setAdChoicesPlacement()
The AdChoices overlay is set to the top right corner by default. Apps can change which corner this overlay is rendered in by setting this property to one of the following:
- ADCHOICES_TOP_LEFT
- ADCHOICES_TOP_RIGHT
- ADCHOICES_BOTTOM_RIGHT
- ADCHOICES_BOTTOM_LEFT
setVideoOptions()
Apps can use this method to set options for video assets returned as part of a native ad. For more information, see Native video ads.
setMediaAspectRatio()
This sets the aspect ratio for image or video to be returned for the native ad. Setting NativeMediaAspectRatio to one of the following constants will cause only ads with media of the specified aspect ratio to be returned:
NATIVE_MEDIA_ASPECT_RATIO_LANDSCAPE
NATIVE_MEDIA_ASPECT_RATIO_PORTRAIT
NATIVE_MEDIA_ASPECT_RATIO_SQUARE
NATIVE_MEDIA_ASPECT_RATIO_ANY
If NativeMediaAspectRatio
is not set, ads with any aspect ratio will be
returned.