Skip to main content

Native Ads

1. Load an ad

  • Developers can preload ads before displaying them.

  • To request a native ad, you need to declare a TPNative object first, set the listener, and load the native ad.

    TPNative tpNative = new TPNative(activity, "AdUnitID");
    tpNative.setAdListener(new NativeAdListener());
    tpNative.loadAd();

2. Show Native Ad

  • It is recommended to use the isReady() method to check if ads are ready; if it returns true, you can then display them.

  • AdContainer is the container for displaying ads. TP will add the loaded ad to the container.

  • layoutId is the layout file. The default layout file is provided in the TradPlusSDK download platform zip; the developer can change the layout style but cannot change the android:id resource ID.

    if (tpNative.isReady()) {
    tpNative.showAd(adContainer, layoutId);
    }

3. Register Ad Event Callback

  • Note: Don't retry loading the ad in the onAdFailed() callback – it'll cause a lot of unnecessary requests and could slow down your app.

    tpNative.setAdListener(new NativeAdListener() {

    @Override
    // Callback when the first ad source is loaded successfully; each load will only trigger the callback once
    public void onAdLoaded(TPAdInfo tpAdInfo, TPBaseAd tpBaseAd) {}

    @Override
    // Native ad clicked
    public void onAdClicked(TPAdInfo tpAdInfo) {}

    @Override
    // Native ad appears on the screen
    public void onAdImpression(TPAdInfo tpAdInfo) {}

    @Override
    // Native ad failed to load
    public void onAdLoadFailed(TPAdError tpAdError) {}

    @Override
    // Native ad show failed (supported by some advertising platforms)
    public void onAdShowFailed(TPAdError tpAdError, TPAdInfo tpAdInfo) {}

    @Override
    // Native ad closed
    public void onAdClosed(TPAdInfo tpAdInfo) {}
    });

4. Code

  • The resources of the advertising SDK cannot be obfuscated. If you use a third-party resource optimization framework, please configure the Resource Whitelist for the SDK.
  • We recommend that you use the NativeActivity to understand how to use the SDK.