Adaptive Banner Ad (Only for GAM)

1

Create your AdRequestConfiguration

Java

Java
AdRequestConfiguration.Builder configuration = AdRequestConfiguration.Companion.builder(context, "Your_placement_name");

Kotlin

Kotlin
val configuration = AdRequestConfiguration.builder(context, "Your_placement_name")
2

Add preferred adaptive ad type to the AdRequest

  • Inline adaptive banner ad https://docs.adster.tech/feature-only-for-gam/adaptive-banner-ad-only-for-gam#inline-adaptive-banner-a-d

    Java / Kotlin
    configuration.addInlineAdaptiveBannerAdSize(width, maxHeight);
  • Current orientation inline adaptive banner ad https://docs.adster.tech/feature-only-for-gam/adaptive-banner-ad-only-for-gam#current-orientation-inline-adaptive-banner-a-d

    Java / Kotlin
    configuration.addCurrentOrientationInlineAdaptiveBannerAdSize(width);
  • Anchored adaptive banner ad https://docs.adster.tech/feature-only-for-gam/adaptive-banner-ad-only-for-gam#anchored-adaptive-banner-a-d

    Java / Kotlin
    configuration.addAnchoredAdaptiveBannerAdSize(getAdSize());
circle-info

Use the below code to get ad size for anchored adaptive banner ad request.

Java

Java
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);
}

Kotlin

Kotlin
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()
}
3

Load the ad

Java

Java
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
    }

    @Override
    public void onAdRevenuePaid(double revenue, @NotNull String adUnitId, @NotNull String network) {
        // Callback which provides revenue and the network which provided it
    }
}).build().loadAd(configuration.build());

Kotlin

Kotlin
AdSterAdLoader.builder().withAdsListener(object : MediationAdListener() {
    override fun onBannerAdLoaded(ad: MediationBannerAd) {
        super.onBannerAdLoaded(ad)
        // Show banner ad here
    }

    override fun onFailure(adError: AdError) {
        // Handle failure here
    }
}).withAdsEventsListener(object : AdEventsListener() {
    override fun onAdClicked() {
        //Handle ad click here
    }

    override fun onAdImpression() {
        //Handle ad impression here
    }

    override fun onAdRevenuePaid(revenue: Double, adUnitId: String, network: String) {
        // Callback which provides revenue and the network which provided it
    }
}).build().loadAd(configuration.build())
4

Add the banner view when loaded

Inside the onBannerAdLoaded callback invoke getView() of the MediationBannerAd object and add the AdSter banner view to your layout:

Java / Kotlin
container.removeAllViews();
container.addView(ad.getView());
5

Destroy the banner

Call MediationBannerAd.destroy() when the activity/fragment is destroyed or detached.

For more details on adaptive banners see:

  • https://docs.adster.tech/feature-only-for-gam/adaptive-banner-ad-only-for-gam#inline-adaptive-banner-a-d

  • https://docs.adster.tech/feature-only-for-gam/adaptive-banner-ad-only-for-gam#current-orientation-inline-adaptive-banner-a-d

  • https://docs.adster.tech/feature-only-for-gam/adaptive-banner-ad-only-for-gam#anchored-adaptive-banner-a-d

This page last updated 13 days ago.

Privacy policy: https://adster.tech/pp