Layout without 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 NativeGenericAdNativeAdViewBinder. The NativeAdViewBinder lets you use any FrameLayout descendant as a root ViewGroup.

Root ViewGroups are descendants of the FrameLayout class. They should be defined as the root element for the set of subViews that the data in the native ad will be bound to.

  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 NativeAdViewBinder class and register the subView set.

    ...
    final List<? extends NativeGenericAd> nativeAds = nativeAdUnit.getNativeAds();
    for (final NativeGenericAd nativeAd : nativeAds) {
        final NativeAdView nativeAdView = mLayoutInflater.inflate(R.layout.widget_native_ad, mNativeAdUnitView, false);
        final NativeAdViewBinder viewBinder = new NativeAdViewBinder.Builder(nativeAdView)
            .setAgeView((TextView) nativeAdView.findViewById(R.id.age))
            .setBodyView((TextView) nativeAdView.findViewById(R.id.body))
            .setCallToActionView((Button) nativeAdView.findViewById(R.id.call_to_action))
            .setDomainView((TextView) nativeAdView.findViewById(R.id.domain))
            .setFaviconView((ImageView) nativeAdView.findViewById(R.id.favicon))
            .setFeedbackView((Button) nativeAdView.findViewById(R.id.feedback))
            .setIconView((ImageView) nativeAdView.findViewById(R.id.icon))
            .setMediaView((MediaView) nativeAdView.findViewById(R.id.media))
            .setPriceView((TextView) nativeAdView.findViewById(R.id.price))
            .setRatingView((MyRatingView) nativeAdView.findViewById(R.id.rating))
            .setReviewCountView((TextView) nativeAdView.findViewById(R.id.review_count))
            .setSponsoredView((TextView) nativeAdView.findViewById(R.id.sponsored))
            .setTitleView((TextView) nativeAdView.findViewById(R.id.title))
            .setWarningView((TextView) nativeAdView.findViewById(R.id.warning))
            .build();
            ...
        }
    ...
  5. For each NativeGenericAd that is part of the NativeAdUnit, call the bindNativeAd method to bind the content to each ad in the block. Pass the created NativeAdViewBinder object as a parameter.

    ...
    final List<? extends NativeGenericAd> nativeAds = nativeAdUnit.getNativeAds();
    for (final NativeGenericAd nativeAd : nativeAds) {
        final NativeAdViewBinder viewBinder = ...;
        try {
            nativeAd.bindNativeAd(viewBinder);
        } catch (final NativeAdException e) {
            ...
        }
    }
    ...
  6. 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

private NativeAdUnitView mNativeAdUnitView;

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 NativeAdView nativeAdView = mLayoutInflater.inflate(R.layout.widget_native_ad, mNativeAdUnitView, false);
                final NativeAdViewBinder viewBinder = new NativeAdViewBinder.Builder(nativeAdView)
                        .setAgeView((TextView) nativeAdView.findViewById(R.id.age))
                        .setBodyView((TextView) nativeAdView.findViewById(R.id.body))
                        .setCallToActionView((Button) nativeAdView.findViewById(R.id.call_to_action))
                        .setDomainView((TextView) nativeAdView.findViewById(R.id.domain))
                        .setFaviconView((ImageView) nativeAdView.findViewById(R.id.favicon))
                        .setFeedbackView((Button) nativeAdView.findViewById(R.id.feedback))
                        .setIconView((ImageView) nativeAdView.findViewById(R.id.icon))
                        .setMediaView((MediaView) nativeAdView.findViewById(R.id.media))
                        .setPriceView((TextView) nativeAdView.findViewById(R.id.price))
                        .setRatingView((MyRatingView) nativeAdView.findViewById(R.id.rating))
                        .setReviewCountView((TextView) nativeAdView.findViewById(R.id.review_count))
                        .setSponsoredView((TextView) nativeAdView.findViewById(R.id.sponsored))
                        .setTitleView((TextView) nativeAdView.findViewById(R.id.title))
                        .setWarningView((TextView) nativeAdView.findViewById(R.id.warning))
                        .build();

                nativeAd.bindNativeAd(viewBinder);
                mNativeAdUnitView.addView(nativeAdView());
            }
        } catch (final NativeAdException exception) {
            ...
        }
    }

    ...
});