Skip to main content
Version: v1.1

Banner ads

Prerequisites

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:

AdManagerAdView adView = new AdManagerAdView(this);

adView.setAdSizes(AdSize.BANNER);

adView.setAdUnitId("/361045431/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)

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);
}
}
That's it! Your app is now ready to display banner ads.

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:

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.
}
});
Note: The size of the container in which you place your ad must be at least as big as the banner size. If your container has padding, that effectively decreases the size of your container. In the event that the container cannot fit the banner ad, the banner will not appear, and you will get this warning in the logs:
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)DescriptionAvailabilityAdSize constant
320x50BannerPhones and TabletsBANNER
320x100Large BannerPhones and TabletsLARGE_BANNER
300x250IAB Medium RectanglePhones and TabletsMEDIUM_RECTANGLE
468x60IAB Full-Size BannerTabletsFULL_BANNER
728x90IAB LeaderboardTabletsLEADERBOARD
Provided width x Adaptive heightAdaptive bannerPhones and TabletsN/A
Screen width x 32|50|90Smart bannerPhones and TabletsSMART_BANNER

To define a custom banner size, set your desired AdSize, as shown here:

AdSize adSize = new AdSize(300, 50);