Native ad options

Native ads have many advanced features that allow you to make additional customizations and make the best possible ad experience. This guide shows you how to use the advanced features of native ads.

Prerequisites

Asset controls

Preferred media aspect ratio controls

Media Aspect Ratio Controls let you specify a preference for the aspect ratio of ad creatives.

Call NativeAdOptions.Builder.setMediaAspectRatio() with a NativeAdOptions.MediaAspectRatio value.

  • When unset, the returned ad can have any media aspect ratio.

  • When set, you will be able to improve the user experience by specifying the preferred type of aspect ratio.

The following example instructs the SDK to prefer a return image or video with a specific aspect ratio.

NativeAdOptions nativeAdOptions = new NativeAdOptions.Builder()
                              .setMediaAspectRatio(NativeAdOptions.NATIVE_MEDIA_ASPECT_RATIO_LANDSCAPE)
                              .build();

AdLoader loader = new AdLoader.Builder(this, '/21775744923/example/native')
                              .withNativeAdOptions(nativeAdOptions)
                              .build();

Image download control

Image download control lets you decide if image assets or only URIs are returned by the SDK.

Call NativeAdOptions.Builder.setReturnUrlsForImageAssets() with a boolean value.
  • Image download control are disabled by default.

  • When disabled, the Google Mobile Ads SDK populates both the image and the URI for you.

  • When enabled, the SDK instead populates just the URI, allowing you to download the actual images at your discretion.

The following example instructs the SDK to return just the URI.

NativeAdOptions nativeAdOptions = new NativeAdOptions.Builder()
                                                     .setReturnUrlsForImageAssets(true)
                                                     .build();

AdLoader loader = new AdLoader.Builder(this, '/21775744923/example/native')
                              .withNativeAdOptions(nativeAdOptions)
                              .forNativeAd(nativeAd -> {
                                List<Uri> imageUris = new ArrayList<>();
                                for (Image image : nativeAd.getImages()) {
                                  imageUris.add(image.getUri());
                                }
                              })
                              .build();

Image payload controls

Some ads have a series of images rather than just one. Use this feature to indicate whether your app is prepared to display all the images or just one.

Call NativeAdOptions.Builder.setRequestMultipleImages() with a boolean value.
  • Image payload controls are disabled by default.

  • When disabled, your app instructs the SDK to provide just the first image for any assets that contain a series.

  • When enabled, your app indicates that it is prepared to display all the images for any assets that have more than one.

The following example instructs the SDK to return multiple image assets.

NativeAdOptions nativeAdOptions = new NativeAdOptions.Builder()
                                                     .setRequestMultipleImages(true)
                                                     .build();

AdLoader loader = new AdLoader.Builder(this, '/21775744923/example/native')
                              .withNativeAdOptions(nativeAdOptions)
                              .build();

AdChoices placements

AdChoices position controls

The AdChoices position controls lets you choose which corner to render the AdChoices icon.

Call NativeAdOptions.Builder.setAdChoicesPlacement() with a NativeAdOption.AdChoicesPlacement value.

  • If unset, the AdChoices icon position is set to the top right.

  • If set, AdChoices is placed at the custom position as requested.

The following example demonstrates how to set a custom AdChoices image position.

NativeAdOptions nativeAdOptions = new NativeAdOptions.Builder()
                                                     .setAdChoicesPlacement(NativeAdOptions.ADCHOICES_BOTTOM_RIGHT)
                                                     .build();

AdLoader loader = new AdLoader.Builder(this, '/21775744923/example/native')
                              .withNativeAdOptions(nativeAdOptions)
                              .build();

AdChoices custom view

The AdChoices custom view feature lets you position the AdChoices icon in a custom location. This is different from AdChoices position controls, which only allows specification of one of the four corners.

Call NativeAdView.setAdChoicesView() with a AdChoicesView value.

The following example demonstrates how to set a custom AdChoices view, with the AdChoices icon rendered inside the AdChoicesView.


public void onNativeAdLoaded(NativeAd ad) {
    NativeAdView nativeAdView = new NativeAdView(getApplicationContext());
    AdChoicesView adChoicesView = new AdChoicesView(this);
    nativeAdView.setAdChoicesView(adChoicesView);
}

Video controls

Start mute behavior

The start muted behavior lets you disable or enable a video's starting audio.

Call VideoOptions.Builder.setStartMuted() with a boolean value.
  • The start muted behavior is enabled by default.

  • When disabled, your app requests the video should begin with audio.

  • When enabled, your app requests that the video should begin with audio muted.

The following example shows how to start the video with un-muted audio.

VideoOptions videoOptions = new VideoOptions.Builder()
                                            .setStartMuted(false)
                                            .build();

NativeAdOptions adOptions = new NativeAdOptions.Builder()
                                               .setVideoOptions(videoOptions)
                                               .build();

AdLoader loader = new AdLoader.Builder(this, '/21775744923/example/native')
                              .withNativeAdOptions(adOptions).build();

Custom playback controls

This lets you request custom video input controls to play, pause, or mute the video.

Call VideoOptions.Builder.setCustomControlsRequested() with a boolean value.
  • Custom playback control are disabled by default.

  • When disabled, your video will show SDK rendered input controls.

  • If the ad does have video content and custom controls are enabled, you should then display your custom controls along with the ad, as the ad won't show any controls itself. The controls can then call the relevant methods on the VideoController.

The following example shows how request a video with custom playback controls.

VideoOptions videoOptions = new VideoOptions.Builder()
                                            .setCustomControlsRequested(true)
                                            .build();

NativeAdOptions adOptions = new NativeAdOptions.Builder()
                                               .setVideoOptions(videoOptions)
                                               .build();

AdLoader loader = new AdLoader.Builder(this, '/21775744923/example/native')
                              .withNativeAdOptions(adOptions).build();

Check if custom controls are enabled

Because it's not known at request time whether the returned ad will allow custom video controls, you must check whether it has custom controls enabled.

Java

@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
  MediaContent mediaContent = nativeAd.getMediaContent();
  if (mediaContent != null) {
    VideoController videoController = mediaContent.getVideoController();
    boolean canShowCustomControls = videoController.isCustomControlsEnabled();
  }
}

Kotlin

NativeAd.OnNativeAdLoadedListener { ad ->
  val mediaContent = ad.mediaContent
  if (mediaContent != null) {
    val videoController = mediaContent.videoController
    val canShowCustomControls = videoController.isCustomControlsEnabled
  }
}

Render custom video controls

Render custom video controls using the following best practices:

  1. Render the custom controls view as a child of the native ad view. This approach ensures that open measurement viewability calculations considers the custom controls as a friendly obstruction.
  2. Avoid rendering an invisible overlay over the entire media view. Overlays block clicks on the media view, negatively impacting native ads performance. Instead, create a small overlay that is just large enough to fit the controls.

Custom click gestures

Custom click gestures is a native ads feature that enables swipes on ad views to be registered as ad clicks. It is designed to work with apps that use swipe gestures for content navigation. This guide shows how to enable custom click gestures on your native ads.

Call NativeAdOptions.Builder.enableCustomClickGestureDirection() with a NativeAdOptions.SwipeGestureDirection and a boolean to indicate whether you want taps to be allowed as clicks.

  • Custom click gestures is disabled by default.

  • When disabled, your app will support normal clicking behaviour.

  • When enabled, your app will support custom swipe gestures.

The following example implements a custom swipe gesture to the right and preserves normal tab behavior.

NativeAdOptions adOptions = new NativeAdOptions
    .Builder()
    .enableCustomClickGestureDirection(NativeAdOptions.SWIPE_GESTURE_DIRECTION_RIGHT,
            /* tapsAllowed= */ true)
    .build();

// The following sample ad unit ID has been enabled for custom click gestures
// and can be used for testing.
AdLoader.Builder builder = new AdLoader
    .Builder(this, '/21775744923/example/native')
    .withNativeAdOptions(adOptions);

Listen for swipe gesture events

When a swipe gesture click is recorded, the Google Mobile Ads SDK invokes the onAdSwipeGestureClicked() method on AdListener, in addition to the existing onAdClicked() method.

AdLoader adLoader = builder
    .withAdListener(
      new AdListener() {
        // Called when a swipe gesture click is recorded.
        @Override
        public void onAdSwipeGestureClicked() {
          Log.d(TAG, "A swipe gesture click has occurred.")
        }

        // Called when a swipe gesture click or a tap click is recorded, as
        // configured in NativeAdOptions.
        @Override
        public void onAdClicked() {
          Log.d(TAG, "A swipe gesture click or a tap click has occurred.")
        }
      })
    .build();

Mediation

Custom click gestures only work on native ads that Google Mobile Ads SDK renders. Ad sources that require third-party SDKs for rendering, don't respond to the custom click directions setting.