-
Notifications
You must be signed in to change notification settings - Fork 4
Load Native Ads
Native ads are advertising assets seamlessly integrated into the user interface using components that are native to the platform. They are displayed through the same views you use to construct your app's layouts, allowing for a cohesive user experience. Additionally, these ads can be customized to align with your app's visual design, ensuring they feel like a natural part of the content rather than intrusive advertisements.
flowchart TD
subgraph CASNativeLoader
L((load)) -->|success| R([onNativeAdLoaded])
L -->|fail| F([onNativeAdFailedToLoad])
end
subgraph CASNativeView
R --> Inf[Inflate layout]
Inf -->|manual| P[registerAdAssetViews]
Inf -->|template| T(setAdTemplateSize)
P --> S(setNativeAd)
T --> S
end
S -->|success| I([onAdImpression])
S -->|fail| SF([onNativeAdFailedToShow])
S --> C([onNativeAdClicked])
A com.cleveradssolutions.sdk.nativead.NativeAdContentCallback
class for handling events related to native ad content.
final NativeAdContentCallback nativeAdCallback = new NativeAdContentCallback() {
@Override
public void onNativeAdLoaded(@NonNull NativeAdContent nativeAd, @NonNull AdContentInfo ad) {
keepNativeAdInMemory(nativeAd);
registerNativeAdContent(nativeAd);
}
@Override
public void onNativeAdFailedToLoad(@NonNull AdError error) {
// (Optional) Handle Ad load errors
}
@Override
public void onNativeAdFailedToShow(@NonNull NativeAdContent nativeAd, @NonNull AdError error) {
// (Optional) Handle Ad render errors.
// Called from CASNativeView.setNativeAd(nativeAd)
}
@Override
public void onNativeAdClicked(@NonNull NativeAdContent nativeAd, @NonNull AdContentInfo ad) {
// (Optional) Called when the native ad is clicked by the user.
}
};
Native ads are loaded with the com.cleveradssolutions.sdk.nativead.CASNativeLoader
class.
void loadNativeAd() {
CASNativeLoader adLoader = new CASNativeLoader(MyActivity.this, MyApplication.CAS_ID, nativeAdCallback);
adLoader.setAdChoicesPlacement(AdChoicesPlacement.TOP_LEFT); // by default
adLoader.setStartVideoMuted(true); // by default
adLoader.load();
}
void keepNativeAdInMemory(NativeAdContent nativeAd) {
loadedNativeAds.add(nativeAd);
}
After a call to load()
, a single callback is made to the previously defined NativeAdContentCallback
to deliver the native ad object or report an error.
The CASNativeLoader
does not have an autoload ad mode like other ad formats, so you need to implement error handling and ad reloading manually.
The load()
method takes an additional parameter: the number of ads the SDK should attempt to load for the request. It's not guaranteed that the SDK will return the exact number of ads requested.
int maxNumberOfAdsToLoad = 3;
adLoader.load(maxNumberOfAdsToLoad);
The onNativeAdLoaded
will be called multiple times, once for each ad that is successfully loaded, up to the specified maximum number of ads.
If the load operation fails, the onNativeAdFailedToLoad
will be called once with the error details.
Apps requesting multiple ads should call CASNativeLoader.isLoading
in their callback implementations to determine whether the loading process has finished.
@Override
public void onNativeAdLoaded(@NonNull NativeAdContent nativeAd, @NonNull AdContentInfo ad) {
keepNativeAdInMemory(nativeAd);
registerNativeAdContent(nativeAd);
if (adLoader.isLoading()) {
// The AdLoader is still loading ads.
// Expect more onNativeAdLoaded or onNativeAdFailedToLoad callbacks.
} else {
// The AdLoader has finished loading ads.
}
}
It’s crucial to call the destroy()
method on all loaded native ads, even if they were not used or referenced. This action releases utilized resources and helps prevent memory leaks.
When you invoke the load()
function, the CASNativeLoader
will continue running until the ad finishes loading. This can lead to the NativeAdContentCallback
being triggered after the Activity
has been destroyed or at unexpected times. Therefore, you should implement checks to destroy the loaded ad if you do not plan to display it to the user.
@Override
public void onNativeAdLoaded(@NonNull NativeAdContent nativeAd, @NonNull AdContentInfo ad) {
// If this callback is invoked after the activity is destroyed,
// call destroy and return to avoid potential memory leaks.
// Note: `isDestroyed()` is a method available on Activity.
if (isDestroyed()) {
nativeAd.destroy()
return
}
keepNativeAdInMemory(nativeAd)
registerNativeAdContent(nativeAd)
}
Make sure to destroy all NativeAdContent
references in your activity’s onDestroy()
function:
@Override
public void onDestroy() {
for (NativeAdContent ad : loadedNativeAds) {
ad.destroy();
}
super.onDestroy();
}
🔗 Next
Display Native Ad
- Integration
- Initialization
- Additional mediation steps
- Google Play data disclosure
- App-ads.txt🔗
- App Open Ad
- Banner Ad
- Interstitial Ad
- Rewarded Ad
- Native Ad