Skip to main content

Banners

1. Load an ad

  • Developers can preload ads before displaying them.

  • TpBanner is a ViewGroup; the size and position can be customized. Developers need to add TpBanner to the specified position.

  • Create an advertisement object TPBanner. Some advertisement platforms require an activity to be passed in; otherwise, the advertisement cannot be loaded successfully.

    TPBanner tpBanner = new TPBanner(activity);
    tpBanner.setAdListener(new BannerAdListener());
    tpBanner.loadAd("AdUnitID");

    // It is recommended to use FrameLayout. If you use LinearLayout, you need to set layoutParams while adding the view
    adContainer.addView(tpBanner);

2. Show banner ad

  • After the ad is loaded successfully, TP will add the ad directly to the TpBanner without calling the showAd() method.
  • For Mintegral, Inmobi, and IronSource platforms, it is recommended to specify a height for the parent container storing banner materials, rather than using wrap_content, otherwise display errors may occur due to ad material issues.
  • For TP-ExChange and Yandex platforms, the tpBanner needs to be added to the display container before requesting the ad using adContainer.addView(tpBanner), otherwise it will fail to display even after successful loading.

3. Destroy an ad

  • After the ad does not need to be displayed anymore, you should destroy it:

    tpBanner.onDestroy();
    tpBanner = null;

4. 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.

    tpBanner.setAdListener(new BannerAdListener() {

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

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

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

    @Override
    // Banner ad failed to load
    public void onAdLoadFailed(TPAdError error) {}

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

5. Code