π¨Adaptive Banner Ad (Only for GAM)
Below are the steps to pass predefined custom targeting value in ad request for GAM (Supported by all ad formats)
Create your
AdRequestConfiguration
as per the below format
val configuration = AdRequestConfiguration.Companion.builder(context, "Your_placement_name");
Adding preferred adaptive ad type to AdRequest
Use the below code to get ad size for anchored adaptive banner ad request :
private Integer getAdSize() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int adWidthPixels = displayMetrics.widthPixels;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = this.getWindowManager().getCurrentWindowMetrics();
adWidthPixels = windowMetrics.getBounds().width();
}
float density = displayMetrics.density;
return (int) (adWidthPixels / density);
}
Call
loadAd()
method as per below format
AdSterAdLoader.Companion.builder().withAdsListener(new MediationAdListener() {
@Override
public void onBannerAdLoaded(@NonNull MediationBannerAd ad) {
super.onBannerAdLoaded(ad);
// Show banner ad here
}
@Override
public void onFailure(@NonNull AdError adError) {
// Handle failure here
}
}).withAdsEventsListener(new AdEventsListener() {
@Override
public void onAdClicked() {
//Handle ad click here
}
@Override
public void onAdImpression() {
//Handle ad impression here
}
}).build().loadAd(configuration.build());
Inside the
onBannerAdLoaded
callback method invokegetView()
method ofMediationnBannerAd
object to add an AdSter banner view to the given layout as shown below
container.removeAllViews();
container.addView(ad.getView());
Call
MediationBannerAd.destroy()
When activity/fragment is destroyed or detached.
Last updated
Was this helpful?