Breaking News

Implement Interstitial Ads in Android with Facebook Audience Network

Implement Interstitial Ads in Android with Facebook Audience Network 


(Updated with Android SDK 6.2.0)

Changes are:
  • Change of InterstitialAdListener 
  • load ad with interstitial AdListener





In this post, we implement Facebook Interstitial Ads using the Facebook Audience Network. We will show test ads by Facebook Audience Network on our Android device.

Then Create an app under MyApp section on top-right corner.



Create an App on Facebook Developer console
Create an App 

Then name your app and click create an app.



Name your App on Facebook Developer Console
Name your App 

Then Click on Audience Network from the product section.



Facebook Audience Network
Click on Audience Network


Then click on Get started of monetization and then select a business account.



Monetization and Business account
Monetization and business account.

Then name your first app.



Name your app
Name your app

Then click on Android from platforms.



Click on Android from platform section
Click on Android from platform section


If your app is live then write your app link, else click on the app is not live.



Facebook Developer Console Facebook Audience Network
The app is not Live


Then under creating placement, add a Placement name.

Facebook Developer console Facebook Audience Network
Name your Placement


Then Click on Interstitial Ad from Placements.
Facebook Developer console Facebook Audience Network
Click on Interstitial


Then click on Create placement. and Then copy your placement Id from next Page.

Facebook Developer console Facebook Audience Network
Copy your Placement Id


*Your Placement ad is only working on your app if you added a payment account with your account.*


So, Add a Test Device in the Testing section to show test ads in your Android app without payment account.


Then, click on a Testing section in the left menu on business.facebook.com



Facebook Developer Console Facebook Audience Network
Click on Testing


Under Add Test Device, Enter your test device Id.


For Test Device Id of Android Mobile.





Open Settings -> click on google -> click on Ads -> your AAID is write in bottom of your screen.


Then enter your device id and click on add device.


Then select the ad type of your added device.



Facebook Developer Console Facebook Audience Network
Add Test Device

Then select Interstitial from ads.



Facebook Developer Console Facebook audience Network
Select Ad Type


Now your ad is set up properly. Now open Android Studio and create a new project.


After the Gradle sync Then opens your app-level Gradle file and add Facebook ads dependency.



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



Then Paste your placement Id in your project String.xml file
String.xml
 <string name="Facebook_Interstitial_placement">1449151565259453_1452519068256036</string>  

Then we create a button to show Interstitial Ad on button click.


acivitymain.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <androidx.constraintlayout.widget.ConstraintLayout 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">  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Interstitial"  
     android:id="@+id/button"/>  
 </androidx.constraintlayout.widget.ConstraintLayout>  

Then we create a variable for Button and Interstitial Ad and find Ids in our java file.


MainActivity.java
 private InterstitialAd interstitialAd;  
   Button btn;  
   String TAG = "Tag";  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     btn = findViewById(R.id.button);  

Then create a click listener for button and then initialize your Interstitial Ad in Button Click listener.

MainActivity.java
 package studio.harpreet.myfbtestads;  
 import androidx.appcompat.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.Button;  
 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.AudienceNetworkAds;  
 import com.facebook.ads.InterstitialAd;  
 import com.facebook.ads.InterstitialAdListener;  
 public class MainActivity extends AppCompatActivity {  
   private InterstitialAd interstitialAd;  
   Button btn;  
   String TAG = "Tag";  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     btn = findViewById(R.id.button);  
     AudienceNetworkAds.initialize(this);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         interstitialAd = new InterstitialAd(MainActivity.this, getResources().getString(R.string.Facebook_Interstitial_placement));  
         InterstitialAdListener interstitialAdListener = 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(interstitialAd.buildLoadAdConfig().withAdListener(interstitialAdListener).build()); } }); } }

Now our Interstitial Ad is successfully loaded on button click.


Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

3 comments:

  1. But 4.1 risk violation our apps please help me brother.

    ReplyDelete
  2. This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post free local classifieds

    ReplyDelete
  3. thanks for the tips and information..i really appreciate it.. facebook ads

    ReplyDelete