'Facebook ads for kotlin

i am trying to show facebook ads on my android app written in kotlin but everytime i try to add code it shows errors, i searched everywhere but could not find a kotlin version

    adView = new AdView(this, "YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);

  // Find the Ad Container
  LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);

  // Add the ad view to your activity layout
  adContainer.addView(adView);

  // Request an ad
  adView.loadAd();


Solution 1:[1]

Android Studio provides the converting tool from Java to Kotlin. To find it double click SHIFT and type 'Convert Java to Kotlin'.

adView = AdView(this, "YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50)
// Find the Ad Container
val adContainer = findViewById<LinearLayout>(R.id.banner_container)
// Add the ad view to your activity layout
adContainer.addView(adView)
// Request an ad
adView.loadAd()

or using kotlin extensions

adView = AdView(this, "YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50)
// Find the Ad Container and add the ad view to your activity layout
banner_container.addView(adView)
// Request an ad
adView.loadAd()

Solution 2:[2]

Create a simple java class and paste the below facebook ads code and just call this class where you want to load and show the ad

import android.content.Context;

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.InterstitialAd;
import com.facebook.ads.InterstitialAdListener;

public class facebbokAds {
    InterstitialAd interstitialAd;
    Context mcontxt;

    public facebbokAds(Context mcontxt) {
        this.mcontxt = mcontxt;
        intestialload();
    }



    public void intestialload() {
        interstitialAd = new InterstitialAd(mcontxt, "YOUR_PLACEMENT_ID");
        // Create listeners for the Interstitial Ad
        InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback

            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback

            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
            }

            @Override
            public void onAdLoaded(Ad adstrong text) {
                // Interstitial ad is loaded and ready to be displayed
                // Show the ad
                interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
            }
        };

        // 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());
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tomasznajda
Solution 2 Shahzeb Khan