https://kotlinlang.org logo
a

Alexander

09/25/2022, 11:24 AM
Hello everyone, I want to warn everyone who wants to use NativeAdView in Compose, now there is a problem with the fact that onAdImpression only fires once on the first ad, and on the rest of the screens the ad is rendered but not taken into account. A little later, we found a workaround for ads to be counted, but later there were problems with the fact that clicks were no longer counted in admob and even cancelled. Therefore, consider this if you are given a similar task.
🙏 4
r

rsktash

12/16/2022, 11:16 PM
Hi @Alexander What was the workaround?
a

Alexander

12/17/2022, 10:24 AM
@rsktash
Copy code
private fun View.requestLayoutWithDelay(delayMillis: Long) {
    postDelayed({
        val t = parent?.parent?.parent
        if (t == null) {
            postDelayed({
                val k = parent?.parent?.parent
                if (k != null) {
                    k.requestLayout()
                } else {
                    Timber.d("NativeBanner: parent is null again")
                }
            }, delayMillis)
        } else {
            t.requestLayout()
        }
    }, delayMillis)
}
Copy code
AndroidViewBinding(
    factory = AdmobBannerBinding::inflate,
    update = {
        
        ...
        
        if (it.root.tag != true) {
            it.root.requestLayoutWithDelay(NATIVE_BANNER_REQUEST_LAYOUT_DELAY)
            it.root.tag = true
        }
    },
    modifier = modifier
        .background(Color.Black)
        .fillMaxWidth()
)
r

rsktash

12/17/2022, 10:43 AM
@Alexander Thank you! is it the working solution for now? Did admob team give a solution to your issue?
a

Alexander

12/17/2022, 1:58 PM
@rsktash This solution is currently in production.
@rsktash admob team has not yet provided a solution to the problem.
r

rsktash

12/23/2022, 2:14 PM
Hi @Alexander what about the clicks? Is it ok
a

Alexander

12/26/2022, 2:08 PM
@rsktash Hi, clicks work fine for me.
n

Namhn1495

12/28/2022, 2:24 PM
Hi, i got the same issue with NativeAdView. Decided to not use it this time. I want to ask about the BANNER ad? does it meet the same behavior? my implementation:
Copy code
AndroidView(
                modifier = Modifier
                    .fillMaxWidth(),
                factory = {

                    
                        AdView(context).apply {
                            setAdSize(AdSize.BANNER)
                            adUnitId = unitId

                            adListener = object : AdListener() {
                                override fun onAdImpression() {
                                    super.onAdImpression()
                                    Timber.v("Banner Ad Impressed.")
                                }

                                override fun onAdLoaded() {
                                    super.onAdLoaded()
                                  placeholder.visibility = View.GONE
                                }
                            }
                            loadAd(AdRequest.Builder().build()
                        })
                    }
                }
            )
a

Alexander

12/28/2022, 2:38 PM
@Namhn1495 AdView works better in Compose, so if the test ads work, then you have everything set up correctly.
o

Oya Canli

04/25/2023, 8:09 AM
Hello! I had the same issue with AdManagerAdView wrapped in Compose AndroidView. Thank you very much for the workaround! 🙏 I have a bit improved the solution. The delay was error prone there as usual.. If we call adView.rootView.requestLayout() from onAdLoaded() callback, it works.. So apparently it needs a layout after ad is loaded.
s

Sudhanshu Siddh

05/09/2023, 4:50 AM
Thank you all for the suggestions here. @Oya Canli using
onAdLoaded()
to invoke
adView.rootView.requestLayout()
is an interesting suggestion. I am also in a similar situation, we already have a working version with
delay
logic but it seems that when the app is in the wild the solution works intermittently. Really curious to know what kind of issues did you run into when using
delay
? Can you also shed some light on your implementation of using
onAdLoaded()
? From what I understand you would only display the Ad when you have a successfully fetched an Ad, how does tying
onAdLoaded
to
requestLayout
help in this scenario? If you are fetching Ads for a list, you might already have received an
onAdLoaded
callback before the Ad item comes into view. Anything you share will be very helpful, since at the minute I am contemplating if it would be worth while to pursue the approach for my use case. Thank you again 🙂
o

Oya Canli

06/02/2023, 8:52 PM
Hi @Sudhanshu Siddh, sorry I missed your message. I actually don't know why we need to call requestLayout, I got the idea from Oleksandr's solution above. Source code is obfuscated so I don't know what is going on under the hood. (Maybe he would know more about it?) Delay was sometimes working, sometimes not.. I wrote logs everywhere and compared with recyclerview implementation and guessed that that "delay" is actually waiting for onAdLoaded() . And indeed, it works without exception now. I shared the snippets on SO if you want: https://stackoverflow.com/questions/76100292/ad-impressions-are-not-count-when-admanageradview-is-used-with-jetpack-compose
c

Cấn Văn Nghị

08/27/2023, 4:47 PM
Hi @Alexander What is the value for NATIVE_BANNER_REQUEST_LAYOUT_DELAY?
Hi @Alexander @Oya Canli Do we have other solutions to address the issue now?
o

Oya Canli

08/27/2023, 7:38 PM
I haven't tried another solution since. I haven't used delays. Requesting layout in onAdLoaded worked fine for me. Ad team confirmed that bug is fixed and impressions are called each time.
a

Alexander

08/27/2023, 7:53 PM
@Cấn Văn Nghị
Copy code
private const val NATIVE_BANNER_REQUEST_LAYOUT_DELAY = 1500L
@Oya Canli I left a question on the ad team forum, but they didn’t write to me that they fixed it, from which version did they fix it?
o

Oya Canli

08/27/2023, 8:50 PM
No no, I am misinderstood, sorry. I was talking about ad team of our company :) So the workaround worked, that was what I meant. No, the bug is not fixed yet by Google afaik.
c

Cấn Văn Nghị

08/28/2023, 1:26 AM
Hi @Alexander just want to know, does the workaround still work well on your product?
a

Alexander

08/29/2023, 11:58 AM
@Cấn Văn Nghị Yes it works.
67 Views