3

Is is possible to use native ads (admob) with jetpack compose?

I've been trying to use AndroidView and got to display the ad template but not the ad itself.

val builder = AdLoader.Builder(context, ADMOB_AD_UNIT_ID).forNativeAd {}

AndroidView(
    factory = { context ->
        val adView = LayoutInflater.from(context)
            .inflate(R.layout.ad_unified, null) as NativeAdView
        builder.forNativeAd { nativeAd ->
            adView.apply {
                    layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
                }
            populateNativeAdView(nativeAd, adView)
            val frameLayout = adView.findViewById<LinearLayout>(R.id.ad_frame)
            frameLayout.removeAllViews()
            frameLayout.addView(adView)
        }.build()
        adView
    }
)


val adLoader = builder.withAdListener(object : AdListener() {
    override fun onAdFailedToLoad(loadAdError: LoadAdError) {
        val error =
            """
       domain: ${loadAdError.domain}, code: ${loadAdError.code}, message: ${loadAdError.message}
      """"
        Log.d("ADMOB: ", error)
    }
}).build()
adLoader.loadAd(AdRequest.Builder().build())

}

Is there and example or some tutorial? I've searched but only found examples/tutorials for banner or interstitial ads. The google admob documentation doesn't touch jetpack compose at all.

Gui
  • 31
  • 2

0 Answers0