Layout using a template

To display a native ad block, make sure the NativeAdUnit instance and each of the NativeGenericAd instances in the NativeAdUnit are bound to a specific root ViewGroup that is associated with each ad type. For NativeAdUnit, this is NativeAdUnitView, and for NativeGenericAdNativeBannerView. NativeBannerView lets you use a template instead of customizing the ad block layout.

  1. See the list of required and optional subviews for each type of ad.

    Note.

    If a subView isn't registered for a required asset in a native ad, binding doesn't occur and the ad isn't displayed. NativeAdException will be thrown and it must be handled.

  2. Register a subView of the assets of a native ad block in the NativeAdUnitView class object.

    public void onNativeAdUnitLoaded(@NonNull final NativeAdUnit nativeAdUnit) {
            mNativeAdUnitView = (NativeAdUnitView) findViewById(R.id.native_content_ad_container);
            mNativeAdUnitView.setSponsoredView((TextView) findViewById(R.id.adunit_sponsored));
            ...
    }
  3. Call the bindAdUnit method for the NativeAdUnit object to bind the content to the native ad block. Pass NativeAdUnitView as a parameter.

    public void onNativeAdUnitLoaded(@NonNull final NativeAdUnit nativeAdUnit) {
        ...
        try {
            nativeAdUnit.bindAdUnit(mNativeAdUnitView);
            bindNativeGenericAds(nativeAdUnit);
        } catch (final NativeAdException exception) {
            ...
        }
    }
  4. For each ad that is part of the native ad block, create an instance of the NativeBannerView class and set the loaded ad for it.

    private void bindNativeGenericAds(@NonNull final NativeAdUnit nativeAdUnit) {
        final List<? extends NativeGenericAd> nativeAds = nativeAdUnit.getNativeAds();
        for (final NativeGenericAd nativeAd : nativeAds) {
            final NativeBannerView nativeBannerView = new NativeBannerView(getContext());
            nativeBannerView.setAd(nativeAd);
            mNativeAdUnitView.addView(nativeBannerView);
        }
    }
  5. To get notifications about user interactions with the native ad block (opening or closing the ad or exiting the app), set for the object of the native ad block and for each NativeGenericAd object in the block a listener named NativeAdEventListener and define the methods:

Sample layout of a native ad block

mNativeAdUnitLoader.setNativeAdUnitLoadListener(new NativeAdUnitLoadListener() {
    @Override
    public void onNativeAdUnitLoaded(@NonNull final NativeAdUnit nativeAdUnit) {
        mNativeAdUnitView = (NativeAdUnitView) findViewById(R.id.native_content_ad_container);
        mNativeAdUnitView.setSponsoredView((TextView) findViewById(R.id.adunit_sponsored));

        try {
            nativeAdUnit.bindAdUnit(mNativeAdUnitView);

            final List<? extends NativeGenericAd> nativeAds = nativeAdUnit.getNativeAds();
            for (final NativeGenericAd nativeAd : nativeAds) {
                final NativeBannerView nativeBannerView = new NativeBannerView(getContext());
                nativeBannerView.setAd(nativeAd);
                mNativeAdUnitView.addView(nativeBannerView);
            }
        } catch (final NativeAdException exception) {
            ...
        }
    }

    ...
});