A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/admob/android/banner below:

Banner ads | Android | Google for Developers

Banner ads

Stay organized with collections Save and categorize content based on your preferences.

Banner ads

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.

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

Prerequisites 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 the Google Mobile Ads SDK's test ads work, see Enable test ads.

Define the ad view container

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" />
Set the ad size

Set the 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));
Kotlin
// Request an anchored adaptive banner with a width of 360.
adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360))
Add AdView to the layout

Create an 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);
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)
Load an ad

Once the AdView is in place, the next step is to load an ad. That's done with the loadAd() method in the AdView class. It takes an AdRequest parameter, which holds runtime information, such as targeting info, about a single ad request.

Key Point: Make all calls to the Mobile Ads SDK on the main thread.

Here's an example that shows how to load an ad:

Java
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
Kotlin
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)

If successful, your app is ready to display banner ads.

Tip: You can use ad load calls to build up a cache of preloaded ads before you intend to show them, so that ads can be shown with zero latency when needed. Since ads expire after an hour, you should clear this cache and reload with new ads every hour. 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. The 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.

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:

Kotlin
// 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
Java
// Remove banner from view hierarchy.
if (adView.getParent() instanceof ViewGroup) {
  ((ViewGroup) adView.getParent()).removeView(adView);
}
// Destroy the banner ad resources.
adView.destroy();
// Drop reference to the banner ad.
adView = null;
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
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(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.
    }
});
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.
    }
}

Each of the overridable methods in AdListener corresponds to an event in the lifecycle of an ad.

Overridable methods onAdClicked() The onAdClicked() method is invoked when a click is recorded for an ad. onAdClosed() The 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 for an implementation of the ad listener methods in the Android API Demo app. onAdFailedToLoad() The onAdFailedToLoad() 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. onAdImpression() The onAdImpression() method is invoked when an impression is recorded for an ad. onAdLoaded() The onAdLoaded() method is executed when an ad has finished loading. If you want to delay adding the AdView to your activity or fragment until you're sure an ad will be loaded, for example, you can do so here. onAdOpened() The 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 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.

Enabling 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> and <activity> 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 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.

Additional resources Examples on GitHub Next steps

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 for more details.

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 for more details.

Explore other topics

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-05-10 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-05-10 UTC."],[[["This guide explains how to implement anchored adaptive banner ads in your Android app to maximize ad performance by optimizing ad size."],["Before you begin, complete the Get started guide and remember to always test with test ads to avoid account suspension."],["To display banner ads, add an AdView to your layout, load an ad using the `loadAd()` method, and listen for ad events using an AdListener."],["Ensure hardware acceleration is enabled for optimal video ad display, either globally or for specific activities using the `android:hardwareAccelerated` attribute in your `AndroidManifest.xml`."],["For further optimization, consider using collapsible banners or inline adaptive banners, and explore topics like user privacy and SDK optimization."]]],["This guide explains how to implement anchored adaptive banner ads in Android apps. Key actions include: creating an `AdView`, determining the ad size based on screen width, adding the `AdView` to the app layout, and using `loadAd()` to load ads. It emphasizes using test ads during development with a specific test ad unit ID, setting ad listeners for various events, and enabling hardware acceleration for video ads. Ad refresh and alternative banner types like collapsible and inline adaptive banners are mentioned.\n"]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3