What is Native Ads Manager?

The Native Ads Manager is a way to pre-fetch Native Ads for your app. Use the Native Ads Manager when your user experience involves displaying multiple ads within a short amount of time, such as a horizontal scroll.

Implementation

Ensure you have completed the Audience Network Getting Started and Android Getting Started guides before you proceed.

Add the following line to the top of your activity class to import the Facebook Ads SDK:

import com.facebook.ads.*;

Initialize NativeAdsManager:

...
private NativeAdsManager manager; 
...
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      ...
      // Initialize a NativeAdsManager and request 5 ads
      manager = new NativeAdsManager(this, "YOUR_PLACEMENT_ID", 5);
      manager.setListener(new NativeAdsManager.Listener() {
        @Override
        public void onAdsLoaded() {
          //Ads Loaded callback
        }

        @Override
        public void onAdError(AdError adError) {
          // Ad error callback
        }
    });
  }

The maximum amount of ads per batch is 10 although we may deliver less ads than requested.

Try not to request significantly more ads than you think may be displayed as it may cause your placement to be throttled.

A batch of unique ads will be loaded in response to calling loadAds() and automatically refreshed by NativeAdsManager.

Once the app is ready to show an ad, call the nextNativeAd() method to retrieve an ad from the NativeAdsManager:

NativeAd nativeAd = manager.nextNativeAd();

It is recommended that the app waits to call this method until immediately before rendering the ad content to ensure the best ad for the given context is selected. Cloned ads will be returned if more than getUniqueNativeAdCount() ads are requested.

Note that nextNativeAd() can return null if there is no ad available. In this case please call loadAds() to refresh the loaded ads.

You can render the ad into a custom layout by following the Native Ads guide. Alternatively, you can use a Native Ad Template, or a Horizontal Scroll which displays a list of ads in a horizontal scroll view.

By default, the Native Ads Manager will refresh its ads periodically (every 30 minutes) so that you may initialize Native Ads Manager and leave the content refresh up to the Audience Network SDK. If you have used up all of the ads requested, then you will need to make a new request with the Native Ads Manager.

Caching Ads with Native Ads Manager

Audience Network supports five cache options in native ads as defined in the NativeAd.MediaCacheFlag enum. This enum can be passed in when you call loadAds():

Cache Constants Description

NONE

No pre-caching, default

ICON

Pre-cache ad icon

IMAGE

Pre-cache ad images

VIDEO

Pre-cache ad video

ALL

Pre-cache all (icon, images, and video)

Horizontal Scroll Ads

Horizontal Scroll Ads are easy to implement using the NativeAdScrollView:

...
private NativeAdsManager manager;
private NativeAdScrollView nativeAdScrollView;
...
  @Override
    protected void onCreate(Bundle savedInstanceState) {
      ...
      // Initialize a NativeAdsManager and request 5 ads
      manager = new NativeAdsManager(this, "YOUR_PLACEMENT_ID", 5);
      manager.setListener(new NativeAdsManager.Listener() {
        @Override
        public void onAdsLoaded() {
          // Create a NativeAdScrollView instance with a height of 300
          nativeAdScrollView = new NativeAdScrollView(HScrollActivity.this, manager,
            NativeAdView.Type.HEIGHT_300);
          LinearLayout hscrollContainer = (LinearLayout) findViewById(R.id.hscrollContainer);
          // Add the NativeAdScrollView to the container
          hscrollContainer.addView(nativeAdScrollView);
        }
      ...
      });
      manager.loadAds(NativeAd.MediaCacheFlag.ALL);
    }

Running the code you should see the following:

Next Steps