In this article we will learn about the Facebook interstitials integrate in our android studio app. So first of all we need an account for app id and unit id.
- Create Facebook Developer account.
- Get app id and unit id of ad.
- Add internet permission in manifest file.
- Add meta data in manifest file.
- Paste Code of interstitial ad and remember the test code.
- Build Your app and enjoy.
Manifest.xml
Permission
<uses-permission android:name="android.permission.INTERNET"/>
Metadata
In meta data add your facebook app id. Without app id ads not shown in your app.
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
Build.Gradule
Repository
mavenCentral()
Dependency
implementation 'com.facebook.android:audience-network-sdk:5.+'
Java Code
private InterstitialAd interstitialAd;
interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID"); interstitialAd.loadAd();
private void showAds() { if (interstitialAd == null || !interstitialAd.isAdLoaded()) { return; } if (interstitialAd.isAdInvalidated()) { return; } // Show the ad interstitialAd.show(); }
@Override protected void onDestroy() { if (interstitialAd != null) { interstitialAd.destroy(); } super.onDestroy(); }
Note!! For Testing on Emulator Add this code and remove this code when publish on play console.
// EMulator testing remove this if (BuildConfig.DEBUG) { AdSettings.setTestMode(true); }