Native

Last Updated on: 15 Nov, 2022

INFO

  • If you miss to Initialize the SDK, this method will fail to return a response.
  • When creating a MesonNative object, ensure that the variable is global (holds a strong reference).
  • When creating the MesonNativeAdLoadListener object, ensure that the variable is global (holds a strong reference).
  • When creating the MesonNativeAdListener object, ensure the variable is global (holds a strong reference).

A Native Ad in Meson consists of two parts :

  1. MesonNative - An object used to load the ad and track its lifecycle.
  2. NativeAdContainer - An object used to show the ad and track its events.

Gradle Dependencies

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"

Create Native Ad Template

To create a native ad, first design and create a Native ad template in your app.

Meson SDK on the top-right corner renders the AdChoices Icon. It is highly recommended not to render any essential asset, especially assets with click actions in the top right corner (Ad Choices Icon is 2px by 2px).

The sample Ad template can be downloaded from here. Remember that the template will be defined under MesonNativeAdContainer.

Attach views to NativeAdContainer

INFO

SDK must be initialized before accessing the MesonNativeAdContainer.

Kotlin

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)

Java

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);

Create Native Ad Unit

INFO

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

Kotlin

val nativeAd = MesonNative(this, “AD_UNIT_ID”)

Java

MesonNative nativeAd = new MesonNative(this, “AD_UNIT_ID”);

Register for callbacks

Track the ad lifecycle for native ads. All the available events for native ads are listed below.

Kotlin

nativeAd.setAdListener(object:MesonNativeAdLoadListener(){

  override fun onAdLoadSucceeded(nativeAd: NativeAd) {
    //add the nativeAd to the nativeAdContainer
  }

  override fun onAdLoadFailed(mesonAdRequestStatus: MesonAdRequestStatus) {
  }


})

Java

nativeAd.setAdListener(new MesonNativeAdLoadListener() {

  @Override
  public void onAdLoadSucceeded(@NonNull NativeAd nativeAd) {        
  }

  @Override
  public void onAdLoadFailed(@NonNull MesonAdRequestStatus mesonAdRequestStatus) {    
  }
});

Load Ad

The publisher can request 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.

Kotlin

nativeAd.load()

Java

nativeAd.load();

Set the Native Ad

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.

Kotlin

nativeAdContainer.setNativeAd(nativeAd)

Java

nativeAdContainer.setNativeAd(nativeAd);

Track Native Ad events

Events such as click, impression, video play/pause, and others can be tracked by registering with the Native Ad Listener.

Kotlin

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() {
    }
})

Java

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) {        
    }
});

Recycle Native Ad with Container

Use this method when a recycler view is being used to show a native ad.

Kotlin

  override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
  holder.nativeAdContainer.recycle()

Java

  @Override
  public void onViewRecycled(RecyclerView.ViewHolder holder) {
    holder.nativeAdContainer.recycle();
  }

Destroy Native Ad with Container

 

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.

Kotlin

nativeAdContainer.destroy()

Java

nativeAdContainer.destroy();