Native

Last Updated on: 25 Sep, 2023

INFO

  • If you miss to Initialize the SDK, this method will fail to return a response.
  • Ensure that everything is executed on the main thread.

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

IMPORTANT

Ensure that you use the exact versions to skip facing any errors due to using an older version.

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.8'

Create Native Ad Template

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

Meson provides you with complete flexibility to design your NativeAdView. Design the following assets:

Name Asset Type Is Mandatory? Description
Title TextView Yes The maximum number of characters allowed is 25.
Description TextView No The maximum number of characters allowed is 100.
MediaView MesonMediaView Yes Body of the ad. It can consist of an Image/Video.
AdSponsored TextView Yes The maximum number of characters allowed is 15.
CallToAction(CTA) TextView No The maximum number of characters allowed is 15.
Icon MesonImageView No A small image is present in the ad.
AdChoice MesonImageView No Place adChoices at the right-top of the view.
Rating TextView No Rating of the ad

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

Click here to download the sample Ad template. Remember that the template will be defined under MesonNativeAdContainer.

Attach views to NativeAdContainer

Kotlin

var nativeAdContainer: MesonNativeAdContainer = 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.

After your adunitId is generated, you can find it on the Meson UI. Add adunitId into the MesonInterstitial object and pass the ActivityContext.

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.

Set the Native Ad within the following onAdLoadSucceeded callback.

Kotlin

nativeAd.setAdListener(object: MesonNativeAdLoadListener() { 
    override fun onAdLoadSucceeded(ad: NativeAd) { 
        //add the nativeAd to the nativeAdContainer here:nativeAdContainer.setNativeAd(nativeAd) 
    } 

     override fun onAdLoadFailed(status: MesonAdRequestStatus) { 
    } 
})

Java

nativeAd.setAdListener(new MesonNativeAdLoadListener() { 
    @Override 
    public void onAdLoadSucceeded(@NonNull NativeAd nativeAd) { 
        super.onAdLoadSucceeded(nativeAd); 
    } 

     @Override 
    public void onAdLoadFailed(@NonNull MesonAdRequestStatus mesonAdRequestStatus) { 
        super.onAdLoadFailed(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

//use load() to make your native ad request
nativeAd.load()

Java

nativeAd.load();

Track Native Ad events

Events such as clicks, impressions, 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 onVideoStarted() { 
        super.onVideoStarted(); 
    } 

    @Override 
    public void onVideoResumed() { 
        super.onVideoResumed(); 
    } 

    @Override 
    public void onVideoPaused() { 
        super.onVideoPaused(); 
    } 

    @Override 
    public void onVideoCompleted() { 
        super.onVideoCompleted(); 
    } 

    @Override 
    public void onUserLeftApplication() { 
        super.onUserLeftApplication(); 
    } 

    @Override 
    public void onAdImpression(@Nullable MesonAdData mesonAdData) { 
        super.onAdImpression(mesonAdData); 
    } 

    @Override 
    public void onAdClicked(@NonNull HashMap<String, Object> hashMap) { 
        super.onAdClicked(hashMap); 
    } 
});

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

//add the nativeAd to the nativeAdContainer, place this within the onAdLoadSucceeded callback
nativeAdContainer.setNativeAd(nativeAd)

Java

//add the nativeAd to the nativeAdContainer, place this within the onAdLoadSucceeded callback
nativeAdContainer.setNativeAd(nativeAd);

Recycle Native Ad with Container

Use this method when a recycler view shows 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 the native container is part.

Kotlin

nativeAdContainer.destroy()

Java

nativeAdContainer.destroy();