Skip to content

Android SDK - Banner ads

Banner ads are rectangular ads that take up a small part of your app's layout, usually anchored to the top or bottom of the screen. They stay visible while users interact with your app, so they can earn revenue without interrupting the experience.

Before you start, install and initialize the SDK.

Caution

MDN11 does not currently support banner auto-refresh, so you need to refresh banners manually. Call load periodically, and call show when the banner has loaded.

Refresh only while the banner is on screen. Stop refreshing when the app goes to the background or the user navigates to a screen without the banner, so you don’t request ads nobody sees.

If your current mediation platform (for example MAX, LevelPlay or AdMob) has banner auto-refresh enabled, you have two options: switch to manual refresh as described above, or keep using that platform’s SDK directly for banners until MDN11 supports auto-refresh. Other ad types can still be used via MDN11 in both cases.

1. Get the banner ad object

Get the ad object for your ad unit ID. Call after initializing MDN11 SDK. To use a size other than Standard, set adSize before calling load(). See Banner sizes.

private val banner by lazy { A11.getBannerAd("adUnitId") }

banner.adSize = BannerAdSize.AnchoredAdaptive()

2. Set the listener

Set the listener before calling load(), so you don't miss any events. When onBannerAdLoaded fires, call show() to display the banner.

banner.listener = object : BannerAdListener {
    override fun onBannerAdLoaded(adInfo: AdInfo) {
        banner.show(this@MyActivity)
    }
    override fun onBannerAdLoadFailed(error: LoadError) {
        Log.e("Ads", "Banner load failed: $error")
    }
    override fun onBannerAdShown(adInfo: AdInfo) {}
    override fun onBannerAdShowFailed(adInfo: AdInfo?, error: ShowError) {
        Log.e("Ads", "Banner show failed: $error")
    }
    override fun onBannerAdClicked(adInfo: AdInfo) {}
    override fun onBannerAdRevenue(adInfo: AdInfo) {}
}

3. Add the view and load the ad

Add adView to your layout before calling load(). The view stays the same when the ad inside it changes, so add it only once.

myBannerContainer.addView(banner.adView)
banner.load(this)

Full example

class MyActivity : Activity() {

    private val banner by lazy { A11.getBannerAd("adUnitId") }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        banner.adSize = BannerAdSize.Standard // set before load()
        banner.listener = object : BannerAdListener {
            override fun onBannerAdLoaded(adInfo: AdInfo) {
                banner.show(this@MyActivity)
            }
            override fun onBannerAdLoadFailed(error: LoadError) {
                Log.e("Ads", "Banner load failed: $error")
            }
            override fun onBannerAdShown(adInfo: AdInfo) {}
            override fun onBannerAdShowFailed(adInfo: AdInfo?, error: ShowError) {
                Log.e("Ads", "Banner show failed: $error")
            }
            override fun onBannerAdClicked(adInfo: AdInfo) {}
            override fun onBannerAdRevenue(adInfo: AdInfo) {}
        }

        // Place the stable view in your layout, then load
        myBannerContainer.addView(banner.adView)
        banner.load(this)
    }
}

The banner size sets how much space the ad takes up on screen and how its height is calculated. Set adSize before calling load(). If you don't set it, the banner uses the Standard size.

Adaptive sizes only work with ad networks that support them. See Adaptive banner support.

Size Class Description
Standard (default) BannerAdSize.Standard Classic fixed-size banner, 320×50 dp. The same size on every device, so it's easy to reserve space for.
Anchored adaptive BannerAdSize.AnchoredAdaptive(widthDp) Width of widthDp, or the width the adView is laid out at if you leave it out. The ad network calculates the height from that width.
Inline adaptive BannerAdSize.InlineAdaptive(widthDp, maxHeightDp) Placed inside scrolling content, such as a list or feed. The height varies from ad to ad, up to maxHeightDp. Leave out widthDp to use the adView width, and maxHeightDp to let the network decide.

Adaptive banner support

Which ad networks support the adaptive banner sizes. For SDK and adapter versions, see Supported ad networks.

Ad Network Adaptive Banner
AdMob
mediation
Google Ad Manager
mediation
AppLovin
mediation - MAX
LevelPlay
mediation