Use inline adaptive for scrolling banners

  • Inline adaptive banners allow you to specify a width to determine the optimal ad size and maximize performance by optimizing for each device.

  • Compared to anchored adaptive banners, inline adaptive banners are larger, taller, have variable heights, and are placed in scrolling content.

  • To implement inline adaptive banners, you obtain the device width (or set a custom width) and use a static method on the ad size class to create an inline adaptive ad size object.

  • You can limit the height of inline adaptive banners by using the AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight) method.

Adaptive banners let you specify the width of an ad to determine the optimal ad size for each device. Unlike anchored adaptive banners, inline adaptive banners are larger, taller, and don't use fixed heights. Inline adaptive banners have variable height. These banners might fill the entire screen or a maximum height you specify.

This guide covers creating inline adaptive banners and setting the banner size.

The following image shows inline adaptive banners in scrolling content:

Diagram showing differences in adaptive and inline adaptive banners

Before you begin

Before continuing, make sure you set up banner ads.

To view a full inline adaptive banner example, download the following sample app:

Implement inline adaptive banners

To implement inline adaptive banners, you set the ad size, create an ad request, and load the ad.

To size an inline adaptive banner, determine the ad width. Common approaches include the following:

  • Using the full-screen width.
  • Using the width of the parent layout containing the ad.
  • Adjusting the width to account for device safe areas.

When creating the ad size object, use this calculated width.

Complete the following steps:

  1. Get the width of your device, or set a width:

    Kotlin

    private val adWidth: Int
      get() {
        val displayMetrics = resources.displayMetrics
        val adWidthPixels =
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val windowMetrics: WindowMetrics = this.windowManager.currentWindowMetrics
            windowMetrics.bounds.width()
          } else {
            displayMetrics.widthPixels
          }
        val density = displayMetrics.density
        return (adWidthPixels / density).toInt()
      }
    

    Java

    public int getAdWidth() {
      DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
      int adWidthPixels = displayMetrics.widthPixels;
    
      if (VERSION.SDK_INT >= VERSION_CODES.R) {
        WindowMetrics windowMetrics = this.getWindowManager().getCurrentWindowMetrics();
        adWidthPixels = windowMetrics.getBounds().width();
      }
    
      float density = displayMetrics.density;
      return (int) (adWidthPixels / density);
    }
    
  2. To get an inline adaptive ad size object, use a static method on the ad size class:

    Kotlin

    val adView = AdView(this@MainActivity)
    adView.setAdSize(AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, adWidth))

    Java

    final AdView adView = new AdView(MainActivity.this);
    adView.setAdSize(AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, getAdWidth()));

If you need to update or create line items to use adaptive banners, see Set up line items for adaptive banners.

Set the inline adaptive banner layout

To preload an inline adaptive banner ad for a specific layout, use the following methods:

If your app supports portrait and landscape views, and you want to preload an adaptive banner ad in the current layout, use the AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Context context, int width) method. This method loads an ad in the current layout.

Limit inline adaptive banner height

By default, inline adaptive banners created without a maxHeight value have a maximum height equal to the device height. To limit the inline adaptive banner height, use the AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight) method.