Menu

Template advertising

1. Introduction

Template feed ads are relatively simple to use in native ads. Developers do not need to define the UI layout of internal ads. The SDK will provide a fully rendered View for developers to display at a designated location. As shown in the screenshot below:

Fixed Location displayInformation flow list display
native_one_express_demo_thumb native_list_express_demo_pic_thumb

2. Integration suggestions

   (1) Start the application through ATNative.makeAdRequestto perform loading ads. Request ads in advance so they can be displayed quickly when ads need to be triggered.

   (2) It is recommended to call entryAdScenario(placementId,scenarioId) after entering the displayable advertising scene to count the current advertising space Cache status, specific statistical description can be viewed in Advertising scenarios Data that distinguishes different business scenarios

    (3) It is forbidden to execute the ad loading method in the onNativeAdLoadFail callback, otherwise it will cause a lot of useless requests and may cause the application to freeze.

   (4) Use ATNative.getNativeAd to obtain whether the advertising object is empty at the location where native advertising needs to be displayed: < /p>

  • Empty: Re-execute ATNative.makeAdRequest to load ads
  • Not empty: Display the advertising object of NativeAd. At this time, you can directly execute ATNative.makeAdRequest to preload the next advertisement (You can preload the next ad directly after calling ATNative.getNativeAd)

   (5) It is recommended to configure a preset strategy: to improve the ad loading effect of the first cold start. For specific instructions and access tutorials, see SDK preset strategy usage instructions

3. Sample code

For detailed sample code, please view Demo: Taku SDK Demo. For single information flow ad display, please refer to Demo: NativeAdActivity.java. For information flow list integration method, please refer to Demo. :NativeListActivity.java

3.1 Loading ads

Note:

  • Template ads have their own width and height The ratio can be viewed in the backend of the advertising platform. Try to choose a template with the same or close aspect ratio in the advertising platform backend, and use that width in the code Pass in a high ratio of width and height to load and display ads to obtain the best display effect.
  • The width and height of ATNativeAdView must be consistent with the width and height corresponding to ATAdConst.KEY.AD_WIDTH and ATAdConst.KEY.AD_HEIGHT, otherwise the display may be incomplete or too small. Question
ATNative atNative;
NativeAd mNativeAd;

int adViewWidth;
int adViewHeight;

public void loadNativeAd() {
  if (atNative == null) {
     //Initialize ad loading object
      atNative = new ATNative(this, nativeTakuPlacementID, new ATNativeNetworkListener() {
                @Override
                public void onNativeAdLoaded() {
                    Log.i(TAG, "onNativeAdLoaded");
                }

                @Override
                public void onNativeAdLoadFail(AdError adError) {
                    //Note: It is prohibited to retry the loading method of advertisements in this callback, otherwise it will cause many useless requests and may cause the application to lag

                   //AdError, please refer to https://newdocs.toponad.com/docs/55cxNt
                    Log.i(TAG, "onNativeAdLoadFail:" + adError.getFullErrorInfo());
                }
            });
   }

  Map localMap = new HashMap<>();
    localMap.put(ATAdConst.KEY.AD_WIDTH, adViewWidth);//Unit: px, expected width of display advertisement
    localMap.put(ATAdConst.KEY.AD_HEIGHT, adViewHeight);//Unit: px, expected display advertisement height

  //The following adaptive heights can be set for the platform (provided that the width is set)
  //Pangle
    localMap.put(TTATConst.NATIVE_AD_IMAGE_HEIGHT, 0);
  //gdt(Tencent Ads),ADSize.AUTO_HEIGHT值为-2
    localMap.put(GDTATConst.AD_HEIGHT, ADSize.AUTO_HEIGHT);

    atNative.setLocalExtra(localMap);
  //Initiate advertising request
    atNative.makeAdRequest();

}

3.2 Display advertising

ATNativeAdView mATNativeAdView; //Container that must be created for rendering advertisements

public void showNativeAd(ViewGroup adContainer, int adViewWidth) {
     /*
    In order to calculate the scene arrival rate, relevant information can be consulted“ https://newdocs.toponad.com/docs/1RWLAv “

Call the "Enter Advertising Scene" method when the advertising triggering conditions are met, for example:

**If an advertisement pops up after the cleaning is completed, it will be called at the end of the cleaning;

*1. First call "entryAdScenario"

*2. Can calling "ATNative # checkAdStatus # isReady" display

*3. Finally, call "getNativeAd" to display

*4. Scenario ID is passed in to the backend funnel report to receive scene data (not mandatory)
     */
  ATNative.entryAdScenario("your Native placementID", "your scenarioID");
  if (atNative== null){ 
      return; 
  }
  if(!atNative.checkAdStatus().isReady()){
    return;
  }
  if (mATNativeAdView == null) {
     mATNativeAdView = findViewById(R.id.native_ad_view); //Can be defined in XML layout
  }
  
  NativeAd nativeAd = atNative.getNativeAd();
  //Developers can directly use ATNative # makeAdRequest to initiate preloading of the next advertisement after calling getNativeAd
  //loadNativeAd();
    if (nativeAd != null) {     
      if (mNativeAd != null) {
           mNativeAd.destory();
      }

      mNativeAd = nativeAd;
            mNativeAd.setNativeEventListener(new ATNativeEventListener() {
            @Override
            public void onAdImpressed(ATNativeAdView view, ATAdInfo atAdInfo) {
                //ATAdInfo can distinguish between advertising platforms and obtain advertising space IDs for advertising platforms
                //please refer to https://newdocs.toponad.com/docs/JSLSJN
                Log.i(TAG, "native ad onAdImpressed:\n" + atAdInfo.toString());   
            }

            @Override
            public void onAdClicked(ATNativeAdView view, ATAdInfo atAdInfo) {
                Log.i(TAG, "native ad onAdClicked:\n" + atAdInfo.toString());
            }

            @Override
            public void onAdVideoStart(ATNativeAdView view) {
                Log.i(TAG, "native ad onAdVideoStart");
            }

            @Override
            public void onAdVideoEnd(ATNativeAdView view) {
                Log.i(TAG, "native ad onAdVideoEnd");
            }

            @Override
            public void onAdVideoProgress(ATNativeAdView view, int progress) {
                Log.i(TAG, "native ad onAdVideoProgress:" + progress);
            }
        });

     ATNativePrepareInfo nativePrepareInfo = null;

      if (!mNativeAd.isNativeExpress()) {
            //Self rendering (if you also need to support self rendering advertisements, you can refer to the integration method of self rendering advertisements)
            ....
       } else {
           //Template rendering (template rendering requires this step to be implemented)
            mNativeAd.renderAdContainer(mATNativeAdView, null);
       }

        mNativeAd.prepare(mATNativeAdView, nativePrepareInfo);       
    } 
}

3.3 Close button description

1. Before rendering NativeAd, developers can set up the close button monitoring of template ads

 mNativeAd.setDislikeCallbackListener(new ATNativeDislikeListener() {
                @Override
                public void onAdCloseButtonClick(ATNativeAdView view, ATAdInfo entity) {
                    Log.i(TAG, "native ad onAdCloseButtonClick");
                      //Here, developers can implement the removal operation of advertising views
                }
});

3.4 Destroy ads

public void destroyAd() {
    if (mNativeAd != null) {
        mNativeAd.destory();
    }
}

For detailed template advertising sample code, please refer to: DemoNativeAdActivity class

Previous
Self-rendering shake
Next
Drawadvertising
Last modified: 2025-05-30Powered by