Skip to content

Banner Ads

Banner ads are rectangular ad formats that occupy part of an app’s layout—often at the top or bottom of the screen or inline in scrollable content. They remain visible as users interact with the app, which allows uninterrupted gameplay or use.

This guide shows you how to integrate banner ads into an your app.

Load ad

Create an instance of CTBannerView, passing in the placementId and calling the loadAd method.

java

CTAdRequest adRequest = new CTAdRequest.Builder()
        .setPlacementId("BANNER_PLACEMENT_ID")// [necessary] placementId from carty publisher
        .setAdSize(CTAdRequest.AdSize.BANNER_300_250)// [optional] set banner ad size
        .build();
CTBannerView bannerView = new CTBannerView(context, adRequest);
bannerView.loadAd();
kotlin

val adRequest = CTAdRequest.Builder()
    .setPlacementId("BANNER_PLACEMENT_ID")// [necessary] placementId from carty publisher
    .setAdSize(CTAdRequest.AdSize.BANNER_300_250)// [optional] set banner ad size
    .build()
val bannerView = CTBannerView(context, adRequest)
bannerView.loadAd()
  • About bannerSize
Banner FormatDimension
CTAdRequest.AdSize.BANNER_320_50320 x 50
CTAdRequest.AdSize.BANNER_320_100320 x 100
CTAdRequest.AdSize.BANNER_300_250300 x 250

Register Listener

It is recommended to register a listener before calling the CTBannerView.loadAd method.

java

bannerView.setBannerAdListener(new CTBannerAdListener() {
    @Override
    public void onLoaded(CTBaseAd baseAd) {
    }

    @Override
    public void onLoadFailed(CTAdError adError) {
    }

    @Override
    public void onShown(CTBaseAd baseAd) {
    }

    @Override
    public void onShowFailed(CTBaseAd baseAd, CTAdError adError) {
    }

    @Override
    public void onClicked(CTBaseAd baseAd) {
    }

    @Override
    public void onClosed(CTBaseAd baseAd) {
    }
});
kotlin

bannerView.setBannerAdListener(object : CTBannerAdListener {
    override fun onLoaded(baseAd: CTBaseAd?) {
    }

    override fun onLoadFailed(adError: CTAdError?) {
    }

    override fun onShown(baseAd: CTBaseAd?) {
    }

    override fun onShowFailed(baseAd: CTBaseAd?, adError: CTAdError?) {
    }

    override fun onClicked(baseAd: CTBaseAd?) {
    }

    override fun onClosed(baseAd: CTBaseAd?) {
    }
})

Display ad

You can add bannerView to the layout before loadAd. The default LayoutParams should be WRAP_CONTENT. Once the ad is loaded successfully, it will be displayed automatically.

java

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
        FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;

adContainer.addView(bannerView, params);
kotlin

val params = FrameLayout.LayoutParams(
    FrameLayout.LayoutParams.WRAP_CONTENT,
    FrameLayout.LayoutParams.WRAP_CONTENT
)
params.gravity = Gravity.CENTER

adContainer.addView(
    this,
    params
)

Destroy ad

It is recommended to call this in the onDestroy method of the activity to release resources.

java

bannerView.destroy();
kotlin

bannerView.destroy()

Client-side bidding

After the ad loads successfully, you can obtain ecpm by calling CTBaseAd.getEcpm

java

baseAd.getEcpm();
kotlin

baseAd.ecpm

After winning the ad bid, call onC2SBiddingSuccess and pass in the second-highest bid from this bid.

java

bannerView.onC2SBiddingSuccess("secondPrice", null);
kotlin

bannerView.onC2SBiddingSuccess("$secondPrice", null)

After an ad is lost, onC2SBiddingFailed is called with the highest bid received in the auction.

java

bannerView.onC2SBiddingFailed("topPrice", null);
kotlin

bannerView.onC2SBiddingFailed("$topPrice", null)

Examples on GitHub