SelfRenderingNative - Atmosplay/AtmosplayAds-Android Wiki

Integrate Self Rendering Native Ad

Self rendering is another rendering mode, which has high flexibility, of native ad. You can splice ad style according to your needs to make ad more suitable for your app.

Initialize Native Ad

AtmosplayNativeAd mAtmosplayNativeAd = new AtmosplayNativeAd(mContext, mAppId, mAdUnitId)

Add NativeRender to Set Layout of Ad

Following elements are included in custom layout:

- mainImageId: id that used to display ImageView of ad's main image
- iconImageId: id that used to display iconImageView of ad's icon
- titleId: id that used to display TextView of ad's title
- textId: id that used to display TextView of ad's description
- buttonId: id that used to display "INSTALL NOW" Button
- playerId: id that used to play VideoView of ad's video
ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)
                .mainImageId(R.id.nal_image)
                .iconImageId(R.id.nal_icon)
                .titleId(R.id.nal_title)
                .textId(R.id.nal_description)
                .buttonId(R.id.nal_button)
                .playerId(R.id.nal_player)
                .build();
NativeAdRender nativeAdRender = new NativeAdRender(viewBinder);
mAtmosplayNativeAd.setAdRender(nativeAdRender);

Attention: NativeAdRender must be set in self rendering mode. Otherwise, ad can not be displayed properly.

Add Listener

Add Request Listener Method and Creat Ad's View

 mAtmosplayNativeAd.setNativeAdLoadListener(new NativeAdLoadListener() {
    @Override
    public void onNativeAdLoaded(NativeAd nativeAd) {
        // When request succeeded, creat ad View. mNativeView is parent container of ad View (like LinearLayout in Demo)
        View view = nativeAd.createAdView(YourActivity.this, mNativeView);
        nativeAd.renderAdView(view);
        mNativeView.addView(view);
    }

    @Override
    public void onNativeAdFailed(int errorCode, String message) {
        // When failed to request ad, check "State Code and Description" section of this doc according to errorCode to locate problem quickly
    }
});

Add Impression Listener Method (Optional)

If necessery, you can add listener callback of ad's impression/click as follows:

mNativeView.setNativeEventListener(new NativeEventListener() {
    @Override
    public void onAdImpressed(View view) {
        // Ad has been impressed
    }

    @Override
    public void onAdClicked(View view) {
        // Ad has been clicked
    }
});

Request Ad

mAtmosplayNativeAd.loadAd()

The NativeAdSample file is the sample code of native ad(Self Rendering).