Android Tutorials | How to add banner ads with Facebook Audience Network

Manifest:

<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

build.gradle – dependency

implementation ‘com.facebook.android:audience-network-sdk:5.11.0’

Layout

<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"
    />

Source Code
import com.facebook.ads.AudienceNetworkAds;

import com.facebook.ads.*;

private NativeBannerAd nativeBannerAd;

oncreate()

AudienceNetworkAds.initialize(this);
adView = new AdView(this, "415395656050XXX_649219926001XXX", 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);


adView.setAdListener(new AdListener() {
    @Override
    public void onError(Ad ad, AdError adError) {
        // Ad error callback
        Toast.makeText(MenuActivity.this, "Error: " + adError.getErrorMessage(),
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAdLoaded(Ad ad) {
        // Ad loaded callback
    }

    @Override
    public void onAdClicked(Ad ad) {
        // Ad clicked callback
    }

    @Override
    public void onLoggingImpression(Ad ad) {
        // Ad impression logged callback
    }
});
// Request an ad
adView.loadAd();

Create new property
Create new adspace
Create banner ad
Enable testing device
Phone>Settings>Google>Ads>advertising id

Add a Comment

Your email address will not be published. Required fields are marked *