Adding Banner Ads to your Android App

The Audience Network allows you to monetize your Android apps with Facebook ads. This guide explains how to add banner ads to your app.

Ensure you have completed the Audience Network Getting Started and Android Getting Started guides before you proceed.

Ad Banner Steps

Step 1: Adding a Layout Container for the Banner Ad

Step 2: Implementing the Banner in your Activity

Step 3: Adding an Ad Listener

Initialize the Audience Network SDK

This method was added in the Android Audience Network SDK version 5.1.

Explicit initialization of the Audience Network Android SDK is required for version 5.3.0 and greater. Please refer to this document about how to initialize the Audience Network Android SDK.

Before creating an ad object and loading ads, you should initialize the Audience Network SDK. It is recommended to do this at app launch.

public class YourApplication extends Application {
    public void onCreate() {
        // Initialize the Audience Network SDK

Step 1: Adding a Layout Container for the Banner Ad

In your layout file (for example: /res/layout/activity_main.xml), add a layout that will act as a container for your Ad.
Remember the id you set here as you will be referencing it in the code later.

<?xml version="1.0" encoding="utf-8"?>

Step 2: Implementing the Banner in your Activity

Add the following code at the top of your Activity in order to import the Facebook Ads SDK:


Next, instantiate an AdView object and make a request to load an ad. Since AdView is a subclass of View, you can add it to your view hierarchy just as with any other view:

private AdView adView;

public void onCreate(Bundle savedInstanceState) {
    // Instantiate an AdView object. 
    // NOTE: The placement ID from the Facebook Monetization Manager identifies your App.
    // To get test ads, add IMG_16_9_APP_INSTALL# to your placement id. Remove this when your app is ready to serve real ads.

    adView = new AdView(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);

    // Find the Ad Container
    LinearLayout adContainer = (LinearLayout) findViewById(;

    // Add the ad view to your activity layout

    // Request an ad

If you are building your app for tablet, consider using the AdSize.BANNER_HEIGHT_90 size instead. In all cases, the banner width is flexible with a minimum of 320px.

Lastly, add the following code to your activity's onDestroy() function to release resources the AdView uses:

protected void onDestroy() {
    if (adView != null) {

Once you run the above, you should see something like this:

If you are using the default Google Android emulator, you'll add the following line of code before loading a test ad:
AdSettings.addTestDevice("HASHED ID");.

Use the hashed ID that is printed to logcat when you first make a request to load an ad on a device.

Genymotion and physical devices do not need this step. If you would like to test with real ads, please consult our Testing Guide.

Step 3: Adding an Ad Listener

Now that you have the basic code running, you can set an AdListener to your AdView to listen for specific events:

import android.widget.Toast;

    protected void onCreate(Bundle savedInstanceState) {
        AdListener adListener = new AdListener() {
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                        "Error: " + adError.getErrorMessage(),

            public void onAdLoaded(Ad ad) {
                // Ad loaded callback

            public void onAdClicked(Ad ad) {
                // Ad clicked callback

            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback

        // Request an ad

Ad Banner Sizes

Audience Network supports three ad sizes to be used in your AdView. The Banner unit's width is flexible with a minimum of 320px, and only the height is defined.

Ad Format AdSize Reference Size Recommendation

Standard Banner



This banner is best suited to phones

Large Banner



This banner is best suited to tablets and larger devices

Medium Rectangle



This format is best suited for scrollable feeds or end-of-level screens

Next Steps