To load and show interstitial ads, you should initialize, configure, and add the MediafyInterstitialAdUnit object to the application. Call the loadAd() method to fetch the ad. Once it is successfully loaded, invoke the show() method at any appropriate point of the app flow. The SDK will present the full-screen ad.
MediafyInterstitialAdUnit
loadAd()
show()
private var adUnit: MediafyInterstitialAdUnit? = null private fun createAd() { // 1. Create MediafyInterstitialAdUnit adUnit = MediafyInterstitialAdUnit(this) // 2. Configure ad unit adUnit?.setAdUnitId(AD_UNIT_ID) adView.setAdSizes(MediafyAdSize(WIDTH, HEIGHT)) adUnit?.setInterstitialAdUnitListener(createListener()) // 3. Load ad adUnit?.loadAd() }
Configuration methods for MediafyAdView:
MediafyAdView
setAdUnitId()
setAdSizes()
setInterstitialAdUnitListener()
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 or multiformat ads, you should set the adFormats property to the respective value:
adFormats
// Make ad request for video ad adUnit.setAdUnitFormats(EnumSet.of(MediafyAdUnitFormat.VIDEO)); // Make ad request for both video and banner ads (default behaviour) adUnit.setAdUnitFormats(EnumSet.of(MediafyAdUnitFormat.BANNER, MediafyAdUnitFormat.VIDEO)); // Make ad request for banner ad adUnit.setAdUnitFormats(EnumSet.of(MediafyAdUnitFormat.BANNER));
Once the ad is loaded, you can invoke the show() method at any appropriate point of the app flow to present the full-screen ad. To know when the ad is loaded, you should implement MediafyInterstitialAdUnitListener interface and subscribe to the ad events in its methods.
MediafyInterstitialAdUnitListener
You can optionally subscribe to the ad’s lifecycle events by implementing the MediafyInterstitialAdUnitListener interface. When the listener’s method onAdLoaded is called, the SDK has successfully loaded the ad. Starting from this point, you can use the show() method to display the full-screen ad.
onAdLoaded
private fun createListener(): MediafyInterstitialAdUnitListener { return object : MediafyInterstitialAdUnitListener { override fun onAdLoaded(MediafyInterstitialAdUnit: MediafyInterstitialAdUnit) { // Called when ad loaded Log.d(TAG, "Ad loaded successfully") // 4. Show ad adUnit.show() } override fun onAdDisplayed(MediafyInterstitialAdUnit: MediafyInterstitialAdUnit) { // Called when ad displayed full screen Log.d(TAG, "Ad displayed") } override fun onAdFailed( MediafyInterstitialAdUnit: MediafyInterstitialAdUnit, e: MediafyAdException, ) { // Called when ad failed to load or parse Log.e(TAG, "Ad failed to load: " + e.message, e) } override fun onAdClicked(MediafyInterstitialAdUnit: MediafyInterstitialAdUnit) { // Called when ad clicked } override fun onAdClosed(MediafyInterstitialAdUnit: MediafyInterstitialAdUnit) { // Called when ad closed } } }