Loading

Note.

The number of ads in the SDK response may be less than that specified in the partner interface. This situation should be handled in the app: make sure you don't show Views that have no ads.

  1. Decide how to load images for showing ads. Images can be loaded automatically or manually. The method for loading images is set using the shouldLoadImagesAutomatically parameter of the Builder.
  2. Create the adLoaderConfiguration using the NativeAdLoaderConfiguration class. When initializing an instance of this class, pass the required parameters: blockID and shouldLoadImagesAutomatically.

    BlockId is a unique identifier in the R-M-XXXXXX-Y format, which is assigned in the Partner interface.

  3. Optionally set preferred image size classes. Each class corresponds to a specific set of image formats. Sizes are set using the setImageSizes method.
    final NativeAdLoaderConfiguration adLoaderConfiguration =
        new NativeAdLoaderConfiguration.Builder(blockId, shouldLoadImagesAutomatically)
            .setImageSizes(NativeAdLoaderConfiguration.NATIVE_IMAGE_SIZE_MEDIUM).build();
    Note.

    By default, all image size classes are requested. This means that any size of image can be included in the response from the SDK.

    Image classes and formats
    NATIVE_IMAGE_SIZE_SMALL class
    Fixed size Allowed height to width ratio Description
    Height — 150 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
    A small image that is positioned next to the ad.
    NATIVE_IMAGE_SIZE_MEDIUM class
    Fixed size Allowed height to width ratio Description
    Width — 600 pixels
    • 16:9
    A medium-sized image that is positioned next to the ad.
    Width — 450 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
    Height — 450 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
    Width — 300 pixels
    • 16:9
    NATIVE_IMAGE_SIZE_LARGE class
    Fixed size Allowed height to width ratio Description
    Width — 1080 pixels
    • 16:9
    A large image that is positioned:
    • In the center of the ad and covers the entire width of the ad or most of the width.
    • At the bottom of the ad and covers the entire width of the ad or most of the width.
    Width — 600 pixels
    • 16:9
    Height — 450 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
  4. Use the NativeAdUnitLoader class to create a loader for a block of multiple native ads. Pass an instance of the adLoaderConfiguration class as a parameter.

    Note.

    When loading ads using NativeAdUnitLoader, keep a strong reference to an instance of the NativeAdUnitLoader class. We don't recommend that you start loading ads via NativeAdUnitLoader if a reference to the class instance is only stored in a local variable.

    mNativeAdUnitLoader = new NativeAdUnitLoader(getContext(), adLoaderConfiguration);
  5. Set NativeAdUnitLoadListener for the NativeAdUnitLoader instance and implement the onNativeAdUnitLoaded and onNativeAdUnitFailedToLoad methods to track loading and get native ads.

    mNativeAdUnitLoader.setNativeAdUnitLoadListener(new NativeAdUnitLoadListener() {
            @Override
            public void onNativeAdUnitLoaded(@NonNull final NativeAdUnit nativeAdUnit) {
              ...
            }
    
            @Override
            public void onNativeAdUnitFailedToLoad(@NonNull AdRequestError error) {
              ...
            }
            ...
          }
          ...
    });
  6. Call the loadAdUnit method of the NativeAdUnitLoader class to load ads. Pass an instance of the AdRequest class as a parameter.

    mNativeAdUnitLoader.loadAdUnit(AdRequest.builder().build());
  7. If the ad loaded, the onNativeAdUnitLoaded method is called. If the ad didn't load, onNativeAdUnitFailedToLoad is called.

Example of native ad block loading

The following code demonstrates creating NativeAdUnitLoader, registering a listener, and loading a native ad:

// Creating a configuration.
NativeAdLoaderConfiguration adLoaderConfiguration =
                new NativeAdLoaderConfiguration.Builder(blockId, shouldLoadImagesAutomatically).build();
// Create a loader.
mNativeAdUnitLoader = new NativeAdUnitLoader(getContext(), adLoaderConfiguration);

// Register a listener for tracking events that occur in the ad.
mNativeAdUnitLoader.setNativeAdUnitLoadListener(new NativeAdUnitLoadListener() {
        @Override
        public void onNativeAdUnitLoaded(@NonNull final NativeAdUnit nativeAdUnit) {
            // Bind the native ad unit
        }

        @Override
        public void onNativeAdUnitFailedToLoad(@NonNull AdRequestError error) {
            // Handle the failure by logging, altering the UI, etc.
        }
});
// Loading ads.
mNativeAdUnitLoader.loadAdUnit(AdRequest.builder().build());