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.
MediafyAdView
loadAd()
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) }
Configuration methods for MediafyAdView:
setAdUnitId()
setAdSizes()
setBannerListener()
setAutoRefreshDelay()
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:
VIDEO
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())
You can optionally subscribe to the ad’s lifecycle events by implementing the MediafyAdViewListener interface:
MediafyAdViewListener
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 } } }