Facebook ads integration in Android – Part 2

Hii developer after complete to create a facebook ads account and create Ads to integrate ads you App. if you Complete  Facebook ads integration into Android app – Part 1 let’s start to add Ads in your app if you did not read part-1.

If you Complete Part -1 Let’s to Integration Part -2. Start your android studio and create a project. ad insert these ads in your activity.

Facebook ads integration in Android – Part 2

Step 1 Create Facebook Audience Network Account

Step 2 App Property in this account.

Step 1, step 2 read on Facebook ads integration into Android app – Part 1  to easy to integrate facebook ads account create and create ads easy way. 

Step 3 Create an Android Project

Step 4 :  Create a New Project Project ClickOn  ==> File  ==> NEW ==> New Project

 Step 5:  Integrate Facebook SDK

dependencies {
  
    compile 'com.facebook.android:audience-network-sdk:5.+'
allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

Step 6:  Add Banner Ads Layout and implement it on java file 

Facebook Banner Ads add Layout XML File

<LinearLayout
    android:id="@+id/banner_Main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="0dp"
    android:orientation="vertical" />

 

Java Code For Facebook Banner Ads

private AdView adViewin OnCreate add

 adView = new AdView(this, "enter your add key", AdSize.BANNER_HEIGHT_50);
// Find the Ad Container
LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_Main);
// Add the ad view to your activity layout
adContainer.addView(adView);
// Request an ad
adView.loadAd();

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

 

Facebook  Interstitial Ads Java Code

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.InterstitialAd;
import com.facebook.ads.InterstitialAdListener;
private final String TAG = Career.class.getSimpleName();
private InterstitialAd interstitialAd;

In OnCreate add these

interstitialAd = new InterstitialAd(this, "enter your key hare");
// Set listeners for the Interstitial Ad
interstitialAd.setAdListener(new InterstitialAdListener() {
    @Override
    public void onInterstitialDisplayed(Ad ad) {
        // Interstitial ad displayed callback
        Log.e(TAG, "Interstitial ad displayed.");
    }

    @Override
    public void onInterstitialDismissed(Ad ad) {
        // Interstitial dismissed callback
        Log.e(TAG, "Interstitial ad dismissed.");
    }

    @Override
    public void onError(Ad ad, AdError adError) {
        // Ad error callback
        Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
    }

    @Override
    public void onAdLoaded(Ad ad) {
        // Interstitial ad is loaded and ready to be displayed
        Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
        // Show the ad
        interstitialAd.show();
    }

    @Override
    public void onAdClicked(Ad ad) {
        // Ad clicked callback
        Log.d(TAG, "Interstitial ad clicked!");
    }

    @Override
    public void onLoggingImpression(Ad ad) {
        // Ad impression logged callback
        Log.d(TAG, "Interstitial ad impression logged!");
    }
});

// For auto play video ads, it's recommended to load the ad
// at least 30 seconds before it is shown
interstitialAd.loadAd();

 

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

 

Complete Code For Banner and Interstitial Facebook Ads

XML File Code.

<LinearLayout
    android:id="@+id/banner_Main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp"
    android:orientation="vertical" />

 

Java Class Complete Code For Facebook Ads Integration.

package com.codeplayon.facebookads;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;
import com.facebook.ads.InterstitialAd;
import com.facebook.ads.InterstitialAdListener;

public class MainActivity extends AppCompatActivity {
    private AdView adView;
    private final String TAG = MainActivity.class.getSimpleName();
    private InterstitialAd interstitialAd;

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

        adView = new AdView(this, "enter your add key", AdSize.BANNER_HEIGHT_50);
// Find the Ad Container
        LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_Main);
// Add the ad view to your activity layout
        adContainer.addView(adView);
// Request an ad
        adView.loadAd();


        interstitialAd = new InterstitialAd(this, "enter your key hare");
// Set listeners for the Interstitial Ad
        interstitialAd.setAdListener(new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                Log.e(TAG, "Interstitial ad dismissed.");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
                Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
                Log.d(TAG, "Interstitial ad impression logged!");
            }
        });

// For auto play video ads, it's recommended to load the ad
// at least 30 seconds before it is shown
        interstitialAd.loadAd();
    }

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

}

 


Please follow and like us:
error
Recommended
Hi Everyone in this tutorial I am sharing how to…