Banner ads
Prerequisites
- Complete the Get started guide.
Add AdView to the layout
The first step toward displaying a banner is to place AdView in the layout for the Activity or Fragment in which you'd like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here's an example that shows an activity's AdView.
# main_activity.xml
...
<com.google.android.gms.ads.admanager.AdManagerAdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/premiumAdsAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="/361045431/example/banner">
</com.google.android.gms.ads.admanager.AdManagerAdView>
...
Note the following required attributes:
- ads:adSize - Set this to the ad size you'd like to use. If you don't want to use the standard size defined by the constant, you can set a custom size instead. See the banner size section below for details.
- ads:adUnitId - Set this to the unique identifier given to the ad unit in your app where ads are to be displayed. If you show banner ads in different activities, each would require an ad unit.
You can alternatively create AdView programmatically. We recommend using an adaptive banner ad size for the best performance:
- Java
- Kotlin
AdManagerAdView adView = new AdManagerAdView(this);
// Use an anchored adaptive banner size for best performance.
adView.setAdSizes(AdSize.getLargeAnchoredAdaptiveBannerAdSize(this, 360));
adView.setAdUnitId("/361045431/example/banner");
// TODO: Add adView to your view hierarchy.
val adView = AdManagerAdView(this)
// Use an anchored adaptive banner size for best performance.
adView.setAdSizes(AdSize.getLargeAnchoredAdaptiveBannerAdSize(this, 360))
adView.adUnitId = "/6499/example/banner"
// TODO: Add adView to your view hierarchy.
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 AdManagerAdView class. It takes an AdManagerAdRequest parameter, which holds runtime information (such as targeting info) about a single ad request.
Here's an example that shows how to load an ad in the onCreate() method of an Activity:
MainActivity (excerpt)
- Java
- Kotlin
package ...
import ...
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;
public class MainActivity extends AppCompatActivity {
private AdManagerAdView premiumAdsAdView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
premiumAdsAdView = findViewById(R.id.premiumAdsAdView);
AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
premiumAdsAdView.loadAd(adRequest);
}
}
package ...
import ...
import com.google.android.gms.ads.admanager.AdManagerAdRequest
import com.google.android.gms.ads.admanager.AdManagerAdView
class MainActivity : AppCompatActivity() {
lateinit var premiumAdsAdView : AdManagerAdView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mAdManagerAdView = findViewById(R.id.premiumAdsAdView)
val adRequest = AdManagerAdRequest.Builder().build()
premiumAdsAdView.loadAd(adRequest)
}
}
Ad events
To further customize the behavior of your ad, you can hook onto a number of events in the ad's lifecycle: loading, opening, closing, and so on. You can listen for these events through the AdListener class.
To use an AdListener with AdManagerAdView, call the setAdListener() method:
- Java
- Kotlin
premiumAdsAdView.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.
}
});
premiumAdsAdView.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.
}
}
Banner sizes
W/Ads: Not enough space to show ad. Needs 320x50 dp, but only has 288x495 dp.
The table below lists the standard banner sizes.
| Size in dp (WxH) | Description | Availability | AdSize constant |
|---|---|---|---|
| Provided width x Adaptive height | Anchored adaptive banner (recommended) | Phones and Tablets | getLargeAnchoredAdaptiveBannerAdSize() / getCurrentOrientationAnchoredAdaptiveBannerAdSize() |
| 320x50 | Banner | Phones and Tablets | BANNER |
| 320x100 | Large Banner | Phones and Tablets | LARGE_BANNER |
| 300x250 | IAB Medium Rectangle | Phones and Tablets | MEDIUM_RECTANGLE |
| 468x60 | IAB Full-Size Banner | Tablets | FULL_BANNER |
| 728x90 | IAB Leaderboard | Tablets | LEADERBOARD |
SMART_BANNER is deprecated. Use anchored adaptive banners (e.g. AdSize.getLargeAnchoredAdaptiveBannerAdSize(context, 360)) instead — they are optimized to deliver the best banner performance for each device width.To define a custom banner size, set your desired AdSize, as shown here:
- Java
- Kotlin
AdSize adSize = new AdSize(300, 50);
val adSize = AdSize(300, 50)