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()Get native ad assets
After the ad loaded, 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
| assets | Type |
|---|---|
| title | String |
| subTitle | String |
| iconUrl | String |
| imageUrl | String |
| logoUrl | String |
| callToAction | String |
| rating | String |
| likes | String |
| sponsored | String |
| adChoiceUrl | String |
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.
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.
ctNative.destroy();- kotlin
ctNative.destroy()