Banner / MREC
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:
Type | Size | Description |
---|---|---|
Size_320x50 | width: 320 height: 50 | Regular banner size. |
Size_728x90 | width: 728 height: 90 | Banner size for tablets. |
Size_300x250 | width: 300 height: 250 | MREC banner size. |
BannerRequest.Builder bannerRequestBuilder = new BannerRequest.Builder()
.setSize(...) // Set BannerSize. Required
.setTargetingParams(...) // Set TargatingParams instance
.setPriceFloorParams(...) // Set price floor parameters
.setSessionAdParams(...) // Set SessionAdParams instance
.setLoadingTimeOut(...) // Set loading timeout in milliseconds
.setPlacementId(...) // Set placement id
val bannerRequestBuilder = BannerRequest.Builder()
.setSize(...) // Set BannerSize. Required
.setTargetingParams(...) // Set TargatingParams 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(new BannerRequest.AdRequestListener() {
/**
* Called when BannerRequest was requested successfully
*
* @param request - BannerRequest instance
* @param auctionResult - AuctionResult info
*/
@Override
public void onRequestSuccess(@NonNull BannerRequest request,
@NonNull AuctionResult auctionResult) {
}
/**
* Called when BannerRequest request failed
*
* @param request - BannerRequest instance
* @param error - BMError with additional info about error
*/
@Override
public void onRequestFailed(@NonNull BannerRequest request,
@NonNull BMError error) {
}
/**
* Called when BannerRequest expired
*
* @param request - BannerRequest instance
*/
@Override
public void onRequestExpired(@NonNull BannerRequest request) {
}
});
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:
BannerRequest bannerRequest = bannerRequestBuilder.build();
val bannerRequest = bannerRequestBuilder.build()
Keep ad request
You need to keep reference to
BannerRequest
before callingBannerRequest#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(...);
bannerRequest.request(...)
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 - callbannerRequest.notifyMediationLoss
Destroy BannerRequest
Destroy the BannerRequest
instance if you don't need it anymore.
bannerRequest.destroy();
bannerRequest.destroy()
Don't destroy the
BannerRequest
instance, if it will be used for load theBannerView
instance or if theBannerView
instance loaded with theBannerRequest
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 hasAuctionResult
. It means the ads have been requested successfully.
bannerRequest.getAuctionResult() != null
bannerRequest.auctionResult != null
- Make sure that the
BannerRequest
instance is not expired.
!bannerRequest.isExpired()
!bannerRequest.isExpired
Before executing load
on the BannerView
instance, set up the BannerListener
instance:
BannerView bannerView = new BannerView(...);
bannerView.setListener(new BannerListener() {
/**
* Called when Ad was loaded and ready to be displayed
*
* @param ad - BannerView instance
*/
@Override
public void onAdLoaded(@NonNull BannerView ad) {
}
/**
* Called when Ad failed to load
*
* @param ad - BannerView instance
* @param error - BMError with additional info about error
*/
@Override
public void onAdLoadFailed(@NonNull BannerView ad,
@NonNull BMError error) {
}
/**
* Called when Ad Impression has been tracked
*
* @param ad - BannerView instance
*/
@Override
public void onAdImpression(@NonNull BannerView ad) {
}
/**
* Called when Ad show failed
*
* @param ad - BannerView instance
* @param error - BMError with additional info about error
*/
@Override
public void onAdShowFailed(@NonNull BannerView ad,
@NonNull BMError error) {
}
/**
* Called when Ad has been clicked
*
* @param ad - BannerView instance
*/
@Override
public void onAdClicked(@NonNull BannerView ad) {
}
/**
* Called when Ad expired
*
* @param ad - BannerView instance
*/
@Override
public void onAdExpired(@NonNull BannerView ad) {
}
});
bannerView.load(bannerRequest);
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();
bannerView.canShow()
To display the BannerView
instance, you just need to add it to the layout.
viewGroup.removeAllViews();
viewGroup.addView(bannerView);
viewGroup.removeAllViews()
viewGroup.addView(bannerView)
Destroy BannerView
After ad was successful shown and no longer needed, it can be destroyed.
bannerView.destroy();
bannerView.destroy()
Sample
You can find code examples written in Java and Kotlin: Github Banner
Updated about 1 year ago