Skip to content

Banner Ads

Banner ads are rectangular ad units designed to fit within an app's layout. They remain visible as users engage with the app and can be positioned at the top, bottom, or inline with content. Banner ads support automatic refresh, allowing new ads to load at defined intervals.

Example

To create a banner ad, start by initializing a BoldwinAdView with the following parameters:

  • placementId: A string used to identify the ad placement.
  • adSizes: A list of BoldwinAdSize objects that specify the size of the ad.
  • autoRefreshPeriod: An ad refresh period in seconds.
  • adListener: An ad listener.
  • sizeListener: A size change listener.
    // 1. Create BoldwinAdView
    val adView = BoldwinAdView(this)

    // 2. Configure ad unit
    adView.setPlacementId(PLACEMENT_ID)
    adView.setAdSizes(BoldwinAdSize.BANNER_300x250)
    adView.setAdListener(createListener())
    adView.setSizeListener(createSizeListener())
    adView.setAutoRefreshPeriod(30)

    // 3. Load ad
    adView.loadAd()

    // 4. Add ad view to the app UI
    adWrapperView.addView(adView)

setPlacementId() accepts a string that identifies the ad placement. It's a required parameter.

Note: The autoRefreshPeriod parameter is optional. If not set, ad refresh will be disabled.

Configuration

The method loadAd() loads and displays the ad. It can be called multiple times to refresh the ad.

A single ad unit can be configured to support multiple ad sizes:

    adView.setAdSizes(BoldwinAdSize.BANNER_300x250, BoldwinAdSize.BANNER_320x50)

You can optionally set the ad listener to receive callbacks for ad events:

    private fun createListener(): BoldwinAdViewListener = object : BoldwinAdViewListener {
        override fun onAdLoaded(bannerView: BoldwinAdView?, ad: BoldwinAd?) {
            Log.d(TAG, "Ad loaded: $ad")
        }

        override fun onAdFailed(
            bannerView: BoldwinAdView?,
            error: BoldwinError?
        ) {
            Log.e(TAG, "Ad failed to load: ${error?.message}")
        }

        override fun onAdDisplayed(bannerView: BoldwinAdView?) {
            Log.d(TAG, "Ad displayed")
        }

        override fun onAdClicked(bannerView: BoldwinAdView?) {
            Log.d(TAG, "Ad clicked")
        }

        override fun onAdClosed(bannerView: BoldwinAdView?) {
            Log.d(TAG, "Ad closed")
        }
    }

You can also set the size listener to receive callbacks for ad size changes:

    private fun createSizeListener(): BoldwinAdViewSizeListener {
        return BoldwinAdViewSizeListener { adView, size ->
            Log.d(TAG, "The ad size will change to size: $size")
        }
    }