Ad Size and Ad unit id must be set before loadAd is called

后端 未结 8 1487
后悔当初
后悔当初 2020-12-03 00:52

I have this in my main xml file:



        
8条回答
  •  臣服心动
    2020-12-03 00:59

    For anyone interested here is a Kotlin version.

    
    
    

    And inside the code behind

    //create a new AdView    
    val adView = AdView(this)
    adView.adSize = AdSize.SMART_BANNER
    adView.adUnitId = "your adUnitID goes here or you can use a function"
    adView.visibility = View.GONE
    
    //add it to the layout
    val layout = adContainer
    layout.addView(adView)
    
    //profit
    MobileAds.initialize(this) {}
    val adRequest = AdRequest.Builder().build()
    adView.loadAd(adRequest)
    adView.adListener = object : AdListener(){
        override fun onAdLoaded() {
            adView.visibility = View.VISIBLE
            super.onAdLoaded()
        }
    }
    

提交回复
热议问题