androidkotlinfacebook-android-sdk

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

  • 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()