π¨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
AdRequestConfigurationas per the below format
AdRequestConfiguration.Builder configuration = AdRequestConfiguration.Companion.builder(context, "Your_placement_name");val configuration = AdRequestConfiguration.builder(context, "Your_placement_name")Adding preferred adaptive ad type to AdRequest
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);
}fun getAdSize(): Int {
val displayMetrics = getResources().displayMetrics
var adWidthPixels = displayMetrics.widthPixels
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = this.windowManager.currentWindowMetrics
adWidthPixels = windowMetrics.bounds.width()
}
val density = displayMetrics.density
return (adWidthPixels / density).toInt()
}Call
loadAd()method as per below format
Inside the
onBannerAdLoadedcallback method invokegetView()method ofMediationnBannerAdobject to add an AdSter banner view to the given layout as shown below
Call
MediationBannerAd.destroy()When activity/fragment is destroyed or detached.
Last updated
Was this helpful?