As of v5.6.0 the adView.setAdListener is deprecated. How can we receive onAdLoaded and onError callbacks now?
Solution: Since 5.6, you should use loadAd(AdView.AdViewLoadConfig loadAdConfig)
public void loadAd(AdView.AdViewLoadConfig loadAdConfig)
Loads an ad.
This method always returns immediately. The ad is loaded asynchronously. The control's ad listener will be called when loading finishes or fails.
Java
AdView adView = new AdView(this, "placementId", new AdSize(width, height));
AdListener adListener = new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
};
AdView.AdViewLoadConfig loadAdConfig = adView.buildLoadAdConfig()
.withAdListener(adListener)
.build();
adView.loadAd(loadAdConfig);
Kotlin
val adView = AdView(this, "placementId", AdSize(width, height))
val adListener = object : AdListener {
override fun onError(ad: Ad, adError: AdError) {
}
override fun onAdLoaded(ad: Ad) {
}
override fun onAdClicked(ad: Ad) {
}
override fun onLoggingImpression(ad: Ad) {
}
}
val loadAdConfig = adView.buildLoadAdConfig()
.withAdListener(adListener)
.build()
adView.loadAd(loadAdConfig)
Find more information here.
Banner Ad Code: To resolve Issue of setAdListener Method deprecated
Replace your setAdlistener() method For Banner Ad Using below code:
AdView adView = new AdView(context, context.getString(R.string.str_facebook_banner), AdSize.BANNER_HEIGHT_90);
AdListener adListener = new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
Log.e(TAG, "Fb failed :: " + adError.toString());
}
@Override
public void onAdLoaded(Ad ad) {
Log.e(TAG, "onAdLoaded: ");
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
};
AdView.AdViewLoadConfig loadAdConfig = adView.buildLoadAdConfig()
.withAdListener(adListener)
.build();
adView.loadAd(loadAdConfig);
Interstitial Ad Code: To resolve Issue of setAdListener Method deprecated
You can use below code to replace setAdListener() method for Interstitial Ad:
InterstitialAd interstitialAd = new InterstitialAd(context, context.getString(R.string.str_facebook_interstitial));
InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
@Override
public void onInterstitialDisplayed(Ad ad) {
}
@Override
public void onInterstitialDismissed(Ad ad) {
}
@Override
public void onError(Ad ad, AdError adError) {
Log.e(TAG, "Fb failed :: " + adError.toString());
}
@Override
public void onAdLoaded(Ad ad) {
Log.e(TAG, "onAdLoaded: ");
interstitialAd.show();
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
};
interstitialAd.loadAd(interstitialAd.buildLoadAdConfig()
.withAdListener(interstitialAdListener)
.withCacheFlags(ALL)
.build());
}
Official Reference Link: https://developers.facebook.com/docs/audience-network/changelog-android#5_6_0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With