Mars SDK - Banner Ad Unit

Add Banner

To load and show banner ads, you should initialize, configure, and add the MediafyAdView object to the app's layout and call the loadAd() method.

private fun createAd() {
    // 1. Create MediafyView
    val adView = MediafyAdView(this)

    // 2. Configure ad unit
    adView.setAdUnitId(AD_UNIT_ID)
    adView.setAdSizes(MediafyAdSize(WIDTH, HEIGHT))
    adView.setBannerListener(createListener())
    adView.setAutoRefreshDelay(30)

    // 3. Load ad
    adView.loadAd()

    // 4. Add MediafyAdView to the app UI
    containerForAd.addView(adView)
}

Configure Banner

Configuration methods for MediafyAdView:

  • setAdUnitId() - sets a unique placement identifier generated on the Mars platform's UI. Which one to use depends on your type of Nars account.
  • setAdSizes() - sets desired ad sizes for this placement. It can have many sizes.
  • setBannerListener() - sets subscriber to the ad’s lifecycle events.
  • setAutoRefreshDelay() - time interval, after which the SDK will automatically make a new ad request and show the new ad.

Ensure you provide sizes for your ad unit in code or via settings on the publisher's UI. It's a critical property.

If you need to integrate video ads, you can also use the MediafyAdView object in the same way as for banner ads. The single required change is you should explicitly set the VIDEO ad format via ad unit property:

adView.setAdUnitFormat(MediafyAdUnitFormat.VIDEO)

Once done, the Mars SDK will make ad requests for video ad, and render the respective creatives.

Also, you can set the video placement type and video listener.

adView.videoPlacementType = MediafyVideoPlacementType.IN_BANNER
adView.setBannerVideoListener(createVideoListener())

Manage the Ad States

You can optionally subscribe to the ad’s lifecycle events by implementing the MediafyAdViewListener interface:

private fun createListener(): MediafyAdViewListener {
    return object : MediafyAdViewListener {
        override fun onAdLoaded(MediafyAdView: MediafyAdView) {
            // Called when ad loaded
            Log.d(TAG, "Ad loaded successfully")
        }

        override fun onAdFailed(MediafyAdView: MediafyAdView, e: MediafyAdException) {
            // Called when ad failed to load or parse
            Log.e(TAG, "Ad failed to load: " + e.message, e)
        }

        override fun onAdDisplayed(MediafyAdView: MediafyAdView) {
            // Called when ad displayed
        }

        override fun onAdClicked(MediafyAdView: MediafyAdView) {
            // Called when ad clicked
        }

        override fun onAdClosed(MediafyAdView: MediafyeAdView) {
            // Called when ad closed
        }
    }
}

Additionally, you can subscribe to the video events.

private fun createVideoListener(): MediafyAdViewVideoListener {
    return object : MediafyAdViewVideoListener {
        override fun onVideoCompleted(mediafyAdView: MediafyAdView) {
            // Called when video completed
            Log.d(TAG, "Video completed")
        }

        override fun onVideoPaused(mediafyAdView: MediafyAdView) {
            // Called when video paused
        }

        override fun onVideoResumed(mediafyAdView: MediafyAdView) {
            // Called when video resumed
        }

        override fun onVideoUnMuted(mediafyAdView: MediafyAdView) {
            // Called when video unmuted
        }

        override fun onVideoMuted(mediafyAdView: MediafyAdView) {
            // Called when video muted
        }
    }
}

Contact us today

to enjoy endless opportunities!

Contact Us
+

Contact Us