Template info feed ads are relatively simple to use in native ads. Developers do not need to define the internal ad UI layout. The SDK will provide a fully rendered View for the developer to place at the specified position for display. As shown in the screenshots below:
| Fixed Position Display | Info Feed List Display |
|---|---|
![]() |
![]() |
(1) When the app starts, use ATNative.makeAdRequest to execute ad loading. Request ads in advance so that ads can be quickly displayed when needed.
(2) It is recommended to call entryAdScenario(placementId,scenarioId) after entering a displayable ad scenario to count the cache status of the current Ad Placement. For detailed usage of statistics, see Ad Scenarios for distinguishing data across different business scenarios.
(3) Do not execute the ad loading method in the onNativeAdLoadFail callback, otherwise it will cause many unnecessary requests and may cause the app to lag.
(4) Get the ad object via ATNative.getNativeAd at the position where native ads need to be displayed to check whether it is null:
ATNative.makeAdRequest to load ads.NativeAd ad object. At this point, you can directly call ATNative.makeAdRequest again to preload the next ad (as long as you have called ATNative.getNativeAd, you can directly preload the next ad).(5) It is recommended to configure a preset strategy to improve the ad loading efficiency during the first cold start. For specific instructions and integration tutorials, see SDK Preset Strategy Instructions.
For detailed sample code, refer to Demo: Taku SDK Demo. For displaying a single info feed ad, refer to the NativeAdActivity.java class in the Demo. For info feed list integration, refer to the NativeListActivity.java class in the Demo.
Note:
- Template ads have their own aspect ratios, which can be viewed in the ad platform dashboard. Try to select templates with the same or similar aspect ratios in the ad platform dashboard, and pass the width and height with that aspect ratio in the code for loading and displaying ads to achieve 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 display may be incomplete or too small.
ATNative atNative;
NativeAd mNativeAd;
int adViewWidth;
int adViewHeight;
public void loadNativeAd() {
if (atNative == null) {
// Initialize the 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: Do not execute the ad loading method in this callback for retry, otherwise it will cause many unnecessary requests and may cause the app to lag.
// AdError, please refer to https://newdocs.takuad.com/docs/55cxNt
Log.i(TAG, "onNativeAdLoadFail:" + adError.getFullErrorInfo());
}
});
}
Map localMap = new HashMap<>();
localMap.put(ATAdConst.KEY.AD_WIDTH, adViewWidth);// Unit: px, the width of the desired ad display
localMap.put(ATAdConst.KEY.AD_HEIGHT, adViewHeight);// Unit: px, the height of the desired ad display
// The following can set adaptive height for specific platforms (the width must be set as a prerequisite).
// Pangle (China)
localMap.put(TTATConst.NATIVE_AD_IMAGE_HEIGHT, 0);
// Tencent Ads, ADSize.AUTO_HEIGHT value is -2
localMap.put(GDTATConst.AD_HEIGHT, ADSize.AUTO_HEIGHT);
atNative.setLocalExtra(localMap);
// Initiate ad request
atNative.makeAdRequest();
}
ATNativeAdView mATNativeAdView; // Container that must be created for rendering ads.
public void showNativeAd(ViewGroup adContainer, int adViewWidth) {
/*
To count scenario arrival rate, related info can be found at "https://help.takuad.com/docs/1RWLAv"
Call the “enter ad scenario” method when the ad triggering conditions are met, for example:
** If the ad scenario is to display an ad after a cleanup is finished, call it when the cleanup is finished;
* 1. First call "entryAdScenario"
* 2. Then call "ATNative#checkAdStatus#isReady" to check if it is displayable
* 3. Finally call "getNativeAd" to display
* 4. Passing the scenario ID into scenario will only result in data reaching the dashboard funnel report (optional).
*/
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();
// After calling getNativeAd, developers can directly use ATNative#makeAdRequest to initiate preloading of the next ad.
// 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 ad platforms and obtain the ad placement ID of the ad platform, etc.
// Please refer to https://newdocs.takuad.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 self-rendering ads also need to be supported, refer to the self-rendering ad integration method.)
....
} else {
// Template Rendering (This step needs to be implemented for template rendering.)
mNativeAd.renderAdContainer(mATNativeAdView, null);
}
mNativeAd.prepare(mATNativeAdView, nativePrepareInfo);
}
}
Before rendering NativeAd, developers can set the close button listener for template ads.
mNativeAd.setDislikeCallbackListener(new ATNativeDislikeListener() {
@Override
public void onAdCloseButtonClick(ATNativeAdView view, ATAdInfo entity) {
Log.i(TAG, "native ad onAdCloseButtonClick");
// Here, the developer can implement the removal of the ad View.
}
});
public void destroyAd() {
if (mNativeAd != null) {
mNativeAd.destory();
}
}
For detailed template ad sample code, refer to the NativeAdActivity class in Demo.