Skip to content

Native Ads

Native ads are ad assets that are presented to users through UI components that are native to the platform. They're shown using the same classes you already use in your storyboards, and can be formatted to match your app's visual design.

When a native ad loads, your app receives an ad object that contains its assets, and the app—rather than Carty SDK—is then responsible for displaying them.

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

Load ad

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

java

CTAdRequest adRequest = new CTAdRequest.Builder()
    .setPlacementId("NATIVE_PLACEMENT_ID")// [necessary] placementId from carty publisher
    .setMute(true)// [optional] does video ads mute play, sdk default value true
    .build();
CTNative ctNative = new CTNative(adRequest, new CTNativeLoadListener() {
    
    @Override
    public void onLoaded(CTBaseAd baseAd) {
        onNativeLoaded(baseAd);
    }

    @Override
    public void onLoadFailed(CTAdError adError) {
    }
});
ctNative.loadAd();
kotlin

val adRequest = CTAdRequest.Builder()
    .setPlacementId("NATIVE_PLACEMENT_ID")// [necessary] placementId from carty publisher
    .setMute(true)// [optional] does video ads mute play, sdk default value true
    .build()
val ctNative = CTNative(adRequest, object : CTNativeLoadListener() {
    
    override fun onLoaded(baseAd: CTBaseAd?) {
        onNativeLoaded(baseAd)
    }

    override fun onLoadFailed(adError: CTAdError?) {
    }
})
ctNative.loadAd()

Show ad

To call sdk templates

The native ad placement must select the template from carty publisher, otherwise, the returned view will be empty.

java

View nativeExpressView = ctNative.getNativeAdView(context, new CTNativeAdListener() {
    @Override
    public void onShown(CTBaseAd baseAd) {
    }

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

    @Override
    public void onClicked(CTBaseAd baseAd) {
    }
});

if (nativeExpressView != null) {
    binding.adContainer.addView(nativeExpressView, new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
kotlin

val nativeExpressView = ctNative?.getNativeAdView(context, object : CTNativeAdListener {
    override fun onShown(baseAd: CTBaseAd?) {
    }

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

    override fun onClicked(baseAd: CTBaseAd?) {
    }
})
if (nativeExpressView != null) {
    binding.adContainer.addView(
        nativeExpressView,
        ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
    )
}

To customize the layout

Get native ad assets

You can obtain information on native ad assets for display.

java

public void onNativeLoaded(CTBaseAd baseAd) {
    if (baseAd != null) {
        ICTNativeInfo nativeInfo = baseAd.getNativeInfo();
        String title = nativeInfo.getTitle();
        String subTitle = nativeInfo.getSubTitle();
        String iconUrl = nativeInfo.getIconUrl();
        String imgUrl = nativeInfo.getImageUrl();
        String logoUrl = nativeInfo.getLogoUrl();
        String ctaText = nativeInfo.getCallToAction();
        String rating = nativeInfo.getRating();
        String likes = nativeInfo.getLikes();
        String sponsored = nativeInfo.getSponsored();
        String adChoiceUrl = nativeInfo.getAdChoiceUrl();
    }
}
kotlin

fun onNativeLoaded(baseAd: CTBaseAd?) {
    if (baseAd != null) {
        val nativeInfo: ICTNativeInfo = baseAd.getNativeInfo()
        nativeInfo?.apply {
            val title = nativeInfo.title
            val subTitle = nativeInfo.subTitle
            val iconUrl = nativeInfo.iconUrl
            val imgUrl = nativeInfo.imageUrl
            val logoUrl = nativeInfo.logoUrl
            val ctaText = nativeInfo.callToAction
            val rating = nativeInfo.rating
            val likes = nativeInfo.likes
            val sponsored = nativeInfo.sponsored
            val adChoiceUrl = nativeInfo.adChoiceUrl
        }
    }
}
  • About native ad assets
assetsType
titleString
subTitleString
iconUrlString
imageUrlString
logoUrlString
callToActionString
ratingString
likesString
sponsoredString
adChoiceUrlString

CTMediaView

Native ads requires obtain the CTMediaView and add it to the layout for display.

java

// developers can control the size of the media view displayed by the outer layout
View mediaView = ctNative.getMediaView(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
        FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;

mediaViewContainer.addView(mediaView, params);
kotlin

// developers can control the size of the media view displayed by the outer layout
val mediaView = ctNative.getMediaView(context)
val params = FrameLayout.LayoutParams(
    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
params.gravity = Gravity.CENTER

mediaViewContainer.addView(mediaView, params)

Register listener

Suggests register listener before the ad displayed.

java

List<View> clickViews = new ArrayList<>();
clickViews.add(titleTv);
clickViews.add(subTitleTv);
clickViews.add(iconIv);
clickViews.add(mediaView);
clickViews.add(ctaBtn);
ctNative.registerViewForInteraction(contentView, clickViews, new CTNativeAdListener() {
    @Override
    public void onShown(CTBaseAd baseAd) {
    }

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

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

val clickViews = arrayListOf<View>()
clickViews.add(titleTv)
clickViews.add(subTitleTv)
clickViews.add(iconIv)
clickViews.add(mediaView)
clickViews.add(ctaBtn)
ctNative.registerViewForInteraction(contentView, clickViews, object : CTNativeAdListener {
    override fun onShown(baseAd: CTBaseAd?) {
    }

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

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

Destroy ad

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

java

ctNative.destroy();
kotlin

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

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

ctNative.onC2SBiddingSuccess("$secondPrice", null)

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

java

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

ctNative.onC2SBiddingFailed("$topPrice", null)

Examples on GitHub