Facebook audience network banner ad placement in android

facebook audience network

Using facebook audience network is a great way to earn some extra bucks. Its so easy to integrate and it can provide you some really good performance. Let’s get started.

app’s build.gradle

Add the code from below to your app level build.gradle file.

implementation 'com.facebook.android:audience-network-sdk:6.+'

Application

Now you have to override your Application class of your android app. Example:

public class YourApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the Audience Network SDK
        AudienceNetworkAds.initialize(this);       
    }
}

Layout

In order to show the banner ad you need a container. It can be any layout. But linear layout is preferred.

<LinearLayout
        android:id="@+id/banner_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        />

Load placement id

Create and copy the banner ad placement id from facebook and use it in your activity.

private AdView adView;

@Override
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(R.id.banner_container);

    // Add the ad view to your activity layout
    adContainer.addView(adView);

    // Request an ad
    adView.loadAd();
}

Its done. your ad should be shown like below