Facebook Interstitial Ad into your app android studio on button onclick
Step 1: add Facebook dependencies build.gradle in your project
dependencies {
// fakebook sdk
implementation 'androidx.annotation:annotation:1.0.0'
implementation
'com.facebook.android:audience-network-sdk:6.2.0'
}
step 2: create variables into mainactivity
private InterstitialAd interstitialAd;
step 3:paste this inside oncreate method
AudienceNetworkInitializeHelper.initialize(getApplicationContext());
interstitialAd = new InterstitialAd(this,
getResources().getString(R.string.adID_interstial));
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
// task here when user cancle ad
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!");
}
@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!");
}
};
interstitialAd.loadAd(interstitialAd.buildLoadAdConfig()
.withAdListener(interstitialAdListener)
.build());
step 4:show this on button onclick
showad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (interstitialAd.isAdLoaded()) {
interstitialAd.show();
} else {
interstitialAd.loadAd();
// task here
}
}
});
Comments
Post a Comment