<br />

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](https://developers.google.com/static/admob/images/inline-adaptive.png)

## Before you begin

Before continuing, make sure you [set up banner ads](https://developers.google.com/admob/android/banner.md.txt).

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

- [Java](https://github.com/googleads/googleads-mobile-android-examples/tree/main/java/advanced/InlineAdaptiveBannerExample)
- [Kotlin](https://github.com/googleads/googleads-mobile-android-examples/tree/main/kotlin/advanced/InlineAdaptiveBannerExample)

<br />

## 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

   ```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

   ```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

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

   ### Java

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

<br />

Set an ad size smaller than or equal to the device screen size.
To determine the maximum screen size, use the
[`Resources.getDisplayMetrics`](https://developer.android.com/reference/android/content/res/Resources#getDisplayMetrics())
method. The `AdSize` object dimensions use density-independent pixels (dp). When comparing, convert the
screen size from pixels to dp.
For details about size constraints, see
[Ad request size requirements](https://support.google.com/admob/answer/9905175#ad-request-size-requirement).


## Set the inline adaptive banner layout

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

- [`AdSize.getPortraitInlineAdaptiveBannerAdSize(Context context, int width)`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdSize.md.txt#getPortraitInlineAdaptiveBannerAdSize(android.content.Context,int))

- [`AdSize.getLandscapeInlineAdaptiveBannerAdSize(Context context, int width)`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdSize.md.txt#getLandscapeInlineAdaptiveBannerAdSize(android.content.Context,int))

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)`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdSize.md.txt#getCurrentOrientationInlineAdaptiveBannerAdSize(android.content.Context,int))
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)`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdSize.md.txt#getInlineAdaptiveBannerAdSize(int,int))

method.