Select platform: [Android (beta)
![New](https://developers.google.com/static/admob/images/new.svg)](https://developers.google.com/admob/android/next-gen/banner "View this page for the Android (beta) platform docs.") [Android](https://developers.google.com/admob/android/banner "View this page for the Android platform docs.") [iOS](https://developers.google.com/admob/ios/banner "View this page for the iOS platform docs.") [Unity](https://developers.google.com/admob/unity/banner "View this page for the Unity platform docs.") [Flutter](https://developers.google.com/admob/flutter/banner "View this page for the Flutter platform docs.")

<br />

![](https://developers.google.com/static/admob/images/format-banner.svg)  
[Banner ads](https://support.google.com/admob/answer/9993556) are rectangular ads that occupy a portion of an app's layout. Anchored adaptive banners are fixed aspect ratio ads that stay on screen while users are interacting with the app, either anchored at the top or bottom of the screen.

<br />

This guide covers loading an anchored adaptive banner ad into an Android app.

## Prerequisites

- Complete the [Get started guide](https://developers.google.com/admob/android/quick-start).
- Optional: For an example banner ads implementation, select one of the following sample apps:
  - The [Java](https://github.com/googleads/googleads-mobile-android-examples/tree/main/java/admob/BannerExample), [Kotlin](https://github.com/googleads/googleads-mobile-android-examples/tree/main/kotlin/admob/BannerExample), or [Jetpack Compose](https://github.com/googleads/googleads-mobile-android-examples/tree/main/kotlin/advanced/JetpackComposeDemo) anchored adaptive banner ads example.
  - The [Java](https://github.com/googleads/googleads-mobile-android-examples/tree/main/java/advanced/APIDemo) or [Kotlin](https://github.com/googleads/googleads-mobile-android-examples/tree/main/kotlin/advanced/APIDemo) advanced features demo.

## Always test with test ads

When building and testing your apps, make sure you use test ads rather than
live, production ads. Failure to do so can lead to suspension of your account.

The easiest way to load test ads is to use our dedicated test ad unit ID for
Android banners:

`ca-app-pub-3940256099942544/9214589741`

It's been specially configured to return test ads for every request, and you can
use it in your own apps while coding, testing, and debugging. Just make sure you
replace it with your own ad unit ID before publishing your app.

For more information about how Google Mobile Ads SDK test ads work, see
[Enable test ads](https://developers.google.com/admob/android/test-ads).

## Define the ad view

### XML Layout

Add a view to your layout XML file to serve as the container for your anchored
adaptive banner ad:  

    <!-- Ad view container that fills the width of the screen and adjusts its
        height to the content of the ad. -->
    <FrameLayout
            android:id="@+id/ad_view_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true" />

### Jetpack Compose

1. Include the [JetpackComposeDemo/compose-util](https://github.com/googleads/googleads-mobile-android-examples/tree/main/kotlin/advanced/JetpackComposeDemo)
   module. This module includes helpers for composing the `AdView` object and assets.

2. Compose a `BannerAd` class from the `compose-util` module:


    // Place the ad view at the bottom of the screen.
    Column(modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
      Box(modifier = modifier.fillMaxWidth()) { BannerAd(adView, modifier) }
    }  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/advanced/JetpackComposeDemo/app/src/main/java/com/google/android/gms/example/jetpackcomposedemo/formats/BannerScreen.kt#L61-L64

## Set the ad size

Set the [`AdSize`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdSize) to an
anchored adaptive banner type with a specified width:  

### Java


    // Request an anchored adaptive banner with a width of 360.
    adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360));  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java#L181-L182

### Kotlin


    // Request an anchored adaptive banner with a width of 360.
    adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360))  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt#L149-L150

### Jetpack Compose


    // Set the adaptive banner ad size with a given width.
    val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(LocalContext.current, 360)
    adView.setAdSize(adSize)  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/advanced/JetpackComposeDemo/app/src/main/java/com/google/android/gms/example/jetpackcomposedemo/formats/BannerScreen.kt#L55-L57

## Add `AdView` to the layout

Create an [`AdView`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdView) using the ad size to add to your
app's layout:  

### Java


    // Create a new ad view.
    adView = new AdView(this);
    adView.setAdUnitId(AD_UNIT_ID);
    // Request an anchored adaptive banner with a width of 360.
    adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360));

    // Replace ad container with new ad view.
    adContainerView.removeAllViews();
    adContainerView.addView(adView);  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java#L177-L187

### Kotlin


    // Create a new ad view.
    val adView = AdView(this)
    adView.adUnitId = AD_UNIT_ID
    // Request an anchored adaptive banner with a width of 360.
    adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360))
    this.adView = adView

    // Replace ad container with new ad view.
    binding.adViewContainer.removeAllViews()
    binding.adViewContainer.addView(adView)  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt#L145-L156

### Jetpack Compose


    val adView = remember { AdView(context) }

    // Setup and load the adview.
    // Set the unique ID for this specific ad unit.
    adView.adUnitId = BANNER_AD_UNIT_ID

    // Set the adaptive banner ad size with a given width.
    val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(LocalContext.current, 360)
    adView.setAdSize(adSize)

    // Place the ad view at the bottom of the screen.
    Column(modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
      Box(modifier = modifier.fillMaxWidth()) { BannerAd(adView, modifier) }
    }  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/advanced/JetpackComposeDemo/app/src/main/java/com/google/android/gms/example/jetpackcomposedemo/formats/BannerScreen.kt#L48-L65



## Load an ad

The following example loads a 360-width anchored adaptive banner ad into an
`AdView` object:
**Key Point:** The `loadAd()` method must be called on the main thread.  

### Java

    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java#L191-L192

### Kotlin

    val adRequest = AdRequest.Builder().build()
    adView.loadAd(adRequest)  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt#L160-L161

## Refresh an ad

If you configured your ad unit to refresh, you don't need to request another ad
when the ad fails to load. Google Mobile Ads SDK respects any refresh rate
you specified in the AdMob UI. If you haven't enabled
refresh, issue a new request. For more details on ad unit refresh, such as
setting a refresh rate, see

[Use automatic refresh for Banner ads](https://support.google.com/admob/answer/3245199).

| **Note:** When setting a refresh rate in the AdMob UI, the automatic refresh occurs only if the banner is visible on screen.

## Release an ad resource

When you are finished using a banner ad, you can release the banner ad's
resources.

To release the ad's resource, you remove the ad from the view hierarchy and
drop all its references:  

### Java

    public void destroyBanner() {
      // Remove banner from view hierarchy.
      if (adView != null) {
        View parentView = (View) adView.getParent();
        if (parentView instanceof ViewGroup) {
          ((ViewGroup) parentView).removeView(adView);
        }

        // Destroy the banner ad resources.
        adView.destroy();
      }

      // Drop reference to the banner ad.
      adView = null;
    }  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/BannerSnippets.java#L77-L92

### Kotlin

    fun destroyBanner() {
      // Remove banner from view hierarchy.
      val parentView = adView?.parent
      if (parentView is ViewGroup) {
        parentView.removeView(adView)
      }

      // Destroy the banner ad resources.
      adView?.destroy()

      // Drop reference to the banner ad.
      adView = null
    }  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/BannerSnippets.kt#L65-L78

## Ad events

You can listen for a number of events in the ad's lifecycle, including loading, ad impression and click, as well as ad open and close events. It is recommended to set the callback before loading the banner.  

### Java

    if (adView != null) {
      adView.setAdListener(
          new AdListener() {
            @Override
            public void onAdClicked() {
              // Code to be executed when the user clicks on an ad.
            }

            @Override
            public void onAdClosed() {
              // Code to be executed when the user is about to return
              // to the app after tapping on an ad.
            }

            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError adError) {
              // Code to be executed when an ad request fails.
            }

            @Override
            public void onAdImpression() {
              // Code to be executed when an impression is recorded
              // for an ad.
            }

            @Override
            public void onAdLoaded() {
              // Code to be executed when an ad finishes loading.
            }

            @Override
            public void onAdOpened() {
              // Code to be executed when an ad opens an overlay that
              // covers the screen.
            }
          });
    }  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/BannerSnippets.java#L36-L72

### Kotlin

    adView?.adListener =
      object : AdListener() {
        override fun onAdClicked() {
          // Code to be executed when the user clicks on an ad.
        }

        override fun onAdClosed() {
          // Code to be executed when the user is about to return
          // to the app after tapping on an ad.
        }

        override fun onAdFailedToLoad(adError: LoadAdError) {
          // Code to be executed when an ad request fails.
        }

        override fun onAdImpression() {
          // Code to be executed when an impression is recorded
          // for an ad.
        }

        override fun onAdLoaded() {
          // Code to be executed when an ad finishes loading.
        }

        override fun onAdOpened() {
          // Code to be executed when an ad opens an overlay that
          // covers the screen.
        }
      }  
    https://github.com/googleads/googleads-mobile-android-examples/blob/6252f89e35ae5b73053ed3f74bf551114920b7ad/kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/BannerSnippets.kt#L32-L60

Each of the overridable methods in [`AdListener`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdListener)
corresponds to an event in the lifecycle of an ad.

| Overridable methods ||
|---|---|
| `onAdClicked()` | The [`onAdClicked()`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdListener#public-void-onadclicked) method is invoked when a click is recorded for an ad. |
| `onAdClosed()` | The [`onAdClosed()`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdListener#public-void-onadclosed) method is invoked when a user returns to the app after viewing an ad's destination URL. Your app can use it to resume suspended activities or perform any other work necessary to make itself ready for interaction. Refer to the [AdMob AdListener example](https://github.com/googleads/googleads-mobile-android-examples/blob/main/java/advanced/APIDemo/app/src/main/java/com/google/android/gms/example/apidemo/AdMobAdListenerFragment.java) for an implementation of the ad listener methods in the Android API Demo app. |
| `onAdFailedToLoad()` | The [`onAdFailedToLoad()`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdListener#public-void-onadfailedtoload-loadaderror-aderror) method is the only one that includes a parameter. The error parameter of type `LoadAdError` describes what error occurred. For more information, refer to the [Debugging Ad Load Errors documentation](https://developers.google.com/admob/android/ad-load-errors). |
| `onAdImpression()` | The [`onAdImpression()`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdListener#public-void-onadimpression) method is invoked when an impression is recorded for an ad. |
| `onAdLoaded()` | The [`onAdLoaded()`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdListener#onAdLoaded()) method is executed when an ad has finished loading. If you want to delay adding the [`AdView`](https://developers.google.com/admob/android) to your activity or fragment until you're sure an ad will be loaded, for example, you can do so here. |
| `onAdOpened()` | The [`onAdOpened()`](https://developers.google.com/admob/android/reference/com/google/android/gms/ads/AdListener#public-void-onadopened) method is invoked when an ad opens an overlay that covers the screen. |

## Hardware acceleration for video ads

In order for video ads to show successfully in your banner ad views, [hardware
acceleration](https://developer.android.com/guide/topics/graphics/hardware-accel) must
be enabled.

Hardware acceleration is enabled by default, but some apps may choose to disable
it. If this applies to your app, we recommend enabling hardware acceleration for
`Activity` classes that use ads.

#### Enable hardware acceleration

If your app does not behave properly with hardware acceleration turned on
globally, you can control it for individual activities as well. To enable or
disable hardware acceleration, you can use the `android:hardwareAccelerated`
attribute for the
[`<application>`](https://developer.android.com/guide/topics/manifest/application-element)
and
[`<activity>`](https://developer.android.com/guide/topics/manifest/activity-element)
elements in your `AndroidManifest.xml`. The following example enables hardware
acceleration for the entire app but disables it for one activity:  

    <application android:hardwareAccelerated="true">
        <!-- For activities that use ads, hardwareAcceleration should be true. -->
        <activity android:hardwareAccelerated="true" />
        <!-- For activities that don't use ads, hardwareAcceleration can be false. -->
        <activity android:hardwareAccelerated="false" />
    </application>

See the [Hardware acceleration
guide](https://developer.android.com/guide/topics/graphics/hardware-accel) for more
information about options for controlling hardware acceleration. Note that
individual ad views cannot be enabled for hardware acceleration if the activity
is disabled, so the activity itself must have hardware acceleration enabled.

## Next steps

### Collapsible banners

Collapsible banner ads are banner ads that are initially presented as a larger
overlay, with a button to collapse the ad to a smaller size. Consider using it
to further optimize your performance. See [collapsible banner ads](https://developers.google.com/admob/android/banner/collapsible) for more details.

### Inline adaptive banners

Inline adaptive banners are larger, taller banners compared to anchored adaptive
banners. They are of variable height, and can be as tall as the device screen.
Inline adaptive banners are recommended over anchored adaptive banner ads for
apps that place banner ads in scrollable content. See [inline adaptive
banners](https://developers.google.com/admob/android/banner/inline-adaptive) for more
details.

### Explore other topics

- [User privacy](https://developers.google.com/admob/android/privacy)
- [Optimize SDK initialization and ad loading
  (beta)](https://developers.google.com/admob/android/optimize-initialization)