A Native Ad in Meson consists of two parts :
Add the following to your app's build.gradle file.
implementation "com.google.android.exoplayer:exoplayer-core:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-ui:2.17.1"
implementation "com.squareup.picasso:picasso:2.71828"
To create a native ad, first design and create a Native ad template in your app.
The sample Ad template can be downloaded from here. Please note that the template will be defined under MesonNativeAdContainer.
SDK must be initialized before accessing the MesonNativeAdContainer.
MesonNativeAdContainer nativeAdContainer = view.findViewById(R.id.native_ad_container)
nativeAdContainer.setTitleViewId(R.id.native_ad_title)
nativeAdContainer.setAdChoicesViewId(R.id.native_ad_choices)
nativeAdContainer.setIconViewId(R.id.native_ad_icon)
nativeAdContainer.setMediaViewId(R.id.native_ad_media)
nativeAdContainer.setDescriptionViewId(R.id.native_ad_desc)
nativeAdContainer.setCTAViewId(R.id.native_ad_cta)
nativeAdContainer.setSponsoredViewId(R.id.native_ad_sponsored)
nativeAdContainer.setRatingViewId(R.id.native_ad_rating)
MesonNativeAdContainer nativeAdContainer = view.findViewById(R.id.native_ad_container);
nativeAdContainer.setTitleViewId(R.id.native_ad_title);
nativeAdContainer.setAdChoicesViewId(R.id.native_ad_choices);
nativeAdContainer.setIconViewId(R.id.native_ad_icon);
nativeAdContainer.setMediaViewId(R.id.native_ad_media);
nativeAdContainer.setDescriptionViewId(R.id.native_ad_desc);
nativeAdContainer.setCTAViewId(R.id.native_ad_cta);
nativeAdContainer.setSponsoredViewId(R.id.native_ad_sponsored);
nativeAdContainer.setRatingViewId(R.id.native_ad_rating);
If you miss to Initialize the SDK, this method will fail to return a response.
To create a native ad, first, create a MesonNative object
val nativeAd = MesonNative(this, “AD_UNIT_ID”)
MesonNative nativeAd = new MesonNative(this, “AD_UNIT_ID”);
Track the ad lifecycle for native ads. All the available events for native ads are listed below.
nativeAd.setAdListener(object:MesonNativeAdLoadListener(){
override fun onAdLoadSucceeded(nativeAd: NativeAd) {
//add the nativeAd to the nativeAdContainer
}
override fun onAdLoadFailed(mesonAdRequestStatus: MesonAdRequestStatus) {
}
})
nativeAd.setAdListener(new MesonNativeAdLoadListener() {
@Override
public void onAdLoadSucceeded(@NonNull NativeAd nativeAd) {
}
@Override
public void onAdLoadFailed(@NonNull MesonAdRequestStatus mesonAdRequestStatus) {
}
});
The publisher can request for a native ad by invoking this method. This will request an ad from all demand sources, select a winner, and make it available on the SDK.
nativeAd.load()
nativeAd.load();
The ad is returned to the user in the onAdLoadSucceeded
callback. You must set the ad in the native ad container to start rendering it.
nativeAdContainer.setNativeAd(nativeAd)
nativeAdContainer.setNativeAd(nativeAd);
Events such as click, impression, video play/pause and others can be tracked by registering with the Native Ad Listener.
nativeAdContainer.setNativeAdListener(object : MesonNativeAdListener() {
override fun onAdClicked(params: HashMap<String, Any>) {
}
override fun onAdImpression(mesonAdData: MesonAdData?) {
}
override fun onUserLeftApplication() {
}
override fun onVideoStarted() {
}
override fun onVideoPaused() {
}
override fun onVideoResumed() {
}
override fun onVideoCompleted() {
}
})
nativeAdContainer.setNativeAdListener(new MesonNativeAdListener() {
@Override
public void onVideoCompleted() {
}
@Override
public void onVideoResumed() {
}
@Override
public void onVideoPaused() {
}
@Override
public void onVideoStarted() {
}
@Override
public void onUserLeftApplication() {
}
@Override
public void onAdClicked(@NonNull HashMap<String, Object> hashMap) {
}
@Override
public void onAdImpression(MesonAdData mesonAdData) {
}
});
Use this method when a recycler view is being used to show a native ad.
override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
holder.nativeAdContainer.recycle()
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
holder.nativeAdContainer.recycle();
}
Use this method when the ad container is no longer needed. We recommend using this onDestroy of the Activity of which native container is part of.
nativeAdContainer.destroy()
nativeAdContainer.destroy();