Facebook Banner ads in Android

This Tutorial Helps you to Implement Facebook Banner Ads in your Android App. In the Previous Tutorials, I Explained how to Integrate Admob Ads. If you Want to Learn how to Integrate Admob Ads Check out my tutorials here.

First Add Facebook Audience Network SDK to app-level build.gradle.

//add this in app level build.gradle file inside dependencies



implementation 'com.facebook.android:audience-network-sdk:6.5.0'

Now Let’s design the Banner Ad in activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="We Display Facebook Banner Ads"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:layout_centerInParent="true"/>

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

</RelativeLayout>

In the above Layout, we implemented LinearLayout as Our Banner Layout. Now Let’s See JAVA Code how to load Banner Ad

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();

In the Above Code, we Added ad view to LinearLayout and Loaded the Banner Ad

If you want to Listen to the Ad Events You Can Implement them in this way.

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
        AdListener adListener = new AdListener() {
            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Toast.makeText(
                        MainActivity.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(adView.buildLoadAdConfig().withAdListener(adListener).build());

After Showing the Ad to Users Don’t Forget to Destroy the used ads. Use the Below Code to Destroy the Displayed Ads.

@Override
    protected void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

Now Let’s See the Full MainActivity.java Code.

MainActivity.java

package com.example.facebookadssampleapp;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdListener;
import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private AdView adView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // We Use "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID" to get Test Ads. Replace this with Original
        //banner AdUnit


        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
        AdListener adListener = new AdListener() {
            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Toast.makeText(
                        MainActivity.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(adView.buildLoadAdConfig().withAdListener(adListener).build());

    }

    @Override
    protected void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

}

That’s it guys, I hope You Understood how to Implement Facebook Banner Ads in Android App. If You Want to Test Your Original Ads in your App Use AdSettings.addTestDevice("HASHED ID");, Use this Code before Loading Ads, Replace Hashed ID with your Device Hashed Id.

Check Audience Network Docs For More Information.

Leave a Reply

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

Back to top button
close