Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should replace deprecated Facebook adView setAdListener to get callbacks?

As of v5.6.0 the adView.setAdListener is deprecated. How can we receive onAdLoaded and onError callbacks now?

like image 210
galaxigirl Avatar asked Jan 02 '20 09:01

galaxigirl


2 Answers

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.

like image 51
Son Truong Avatar answered Sep 17 '22 23:09

Son Truong


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

like image 39
Nimesh Patel Avatar answered Sep 20 '22 23:09

Nimesh Patel