Skip to main content

Banner / MREC Ads

BannerRequest

Setup BannerRequest builder

To create a BannerRequest instance, you need to create a BannerRequest.Builder instance, set all the necessary parameters and call build on it.

BannerSize is a required parameter:

TypeSizeDescription
Size_320x50width: 320
height: 50
Regular banner size.
Size_728x90width: 728
height: 90
Banner size for tablets.
Size_300x250width: 300
height: 250
MREC banner size.
val bannerRequestBuilder = BannerRequest.Builder()
.setSize(...) // Set BannerSize. Required
.setTargetingParams(...) // Set TargetingParams instance
.setPriceFloorParams(...) // Set price floor parameters
.setSessionAdParams(...) // Set SessionAdParams instance
.setLoadingTimeOut(...) // Set loading timeout in milliseconds
.setPlacementId(...) // Set placement id

Set AdRequestListener

Set the BannerRequest.AdRequestListener instance to theBannerRequest.Builder instance.

bannerRequestBuilder.setListener(object : BannerRequest.AdRequestListener {

/**
* Called when BannerRequest was requested successfully
*
* @param request - BannerRequest instance
* @param auctionResult - AuctionResult info
*/
override fun onRequestSuccess(request: BannerRequest,
auctionResult: AuctionResult) {

}

/**
* Called when BannerRequest request failed
*
* @param request - BannerRequest instance
* @param error - BMError with additional info about error
*/
override fun onRequestFailed(request: BannerRequest,
error: BMError) {

}

/**
* Called when BannerRequest expired
*
* @param request - BannerRequest instance
*/
override fun onRequestExpired(request: BannerRequest) {

}

})
note

AdRequestListener callbacks are delivered on the background thread, not the main one.

Build BannerRequest

When all the necessary parameters are set, call build on the BannerRequest.Builder instance:

val bannerRequest = bannerRequestBuilder.build()
Keep Ad Request

You need to keep reference to BannerRequest before calling BannerRequest#request, otherwise, it is possible it will be cleared by Garbage Collector and callbacks won’t be triggered.

Request BannerRequest

When you need to request an ad and get an AuctionResult, call request on the BannerRequest instance.

bannerRequest.request(...)
note

If you have an in-house meditation and you decide that an advertisement from BidMachine will be shown - call bannerRequest.notifyMediationWin, if BidMachine loses the mediation - call bannerRequest.notifyMediationLoss

Destroy BannerRequest

Destroy the BannerRequest instance if you don't need it anymore.

bannerRequest.destroy()
warning

Don't destroy the BannerRequest instance, if it will be used for load the BannerView instance or if the BannerView instance loaded with the BannerRequest instance has not been shown yet. Otherwise, ad will not work correctly, which can affect a lower display rate, fill rate, rendering errors, and as a result - lower revenue.

BannerView

Load BannerView

When you would like to load and display requested Ads:

  • Make sure that the BannerRequest instance has AuctionResult. It means the ads have been requested successfully.
bannerRequest.auctionResult != null
  • Make sure that the BannerRequest instance is not expired.
!bannerRequest.isExpired

Before executing load on the BannerView instance, set up the BannerListener instance:

val bannerView = BannerView(...)
bannerView.setListener(object : BannerListener {

/**
* Called when Ad was loaded and ready to be displayed
*
* @param ad - BannerView instance
*/
override fun onAdLoaded(ad: BannerView) {

}

/**
* Called when Ad failed to load
*
* @param ad - BannerView instance
* @param error - BMError with additional info about error
*/
override fun onAdLoadFailed(ad: BannerView,
error: BMError) {

}

/**
* Called when Ad Impression has been tracked
*
* @param ad - BannerView instance
*/
override fun onAdImpression(ad: BannerView) {

}

/**
* Called when Ad show failed
*
* @param ad - BannerView instance
* @param error - BMError with additional info about error
*/
override fun onAdShowFailed(ad: BannerView,
error: BMError) {

}

/**
* Called when Ad has been clicked
*
* @param ad - BannerView instance
*/
override fun onAdClicked(ad: BannerView) {

}

/**
* Called when Ad expired
*
* @param ad - BannerView instance
*/
override fun onAdExpired(ad: BannerView) {

}

})
bannerView.load(bannerRequest)

Use onAdLoaded callback to determine the possibility of displaying

Show BannerView

Before displaying, check if the BannerView instance can be displayed:

bannerView.canShow()

To display the BannerView instance, you just need to add it to the layout.

viewGroup.removeAllViews()
viewGroup.addView(bannerView)

Destroy BannerView

After ad was successful shown and no longer needed, it can be destroyed.

bannerView.destroy()
info

You can find code examples written in Java and Kotlin: Github Banner