googleads-mobile-android-examples icon indicating copy to clipboard operation
googleads-mobile-android-examples copied to clipboard

Bug: samples don't calculate dp values properly

Open AndroidDeveloperLB opened this issue 1 year ago • 2 comments

When looking at the samples, I've noticed their calculation is losing the precision in a wrong way, so they can actually fail to get the ad that fits the size properly:

https://github.com/googleads/googleads-mobile-android-examples/blob/ab797ea740bd4100e61e5130c64cac44695ac6f1/kotlin/advanced/InlineAdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/inlineadaptivebannerexample/MainActivity.kt#L69

https://github.com/googleads/googleads-mobile-android-examples/blob/ab797ea740bd4100e61e5130c64cac44695ac6f1/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt#L50

The conversion should be like this:

val displayMetrics = resources.displayMetrics
val adWidthInDp = TypedValueCompat.deriveDimension(TypedValue.COMPLEX_UNIT_DIP, adWidthPixels.toFloat(), displayMetrics)

AndroidDeveloperLB avatar Feb 04 '25 01:02 AndroidDeveloperLB

Could you explain under what conditions you were getting the wrong value, and what the difference in return value is between the current sample code and the conversion proposal? Is it related to rounding and off-by-1 errors?

That said, the proposed conversion looks reasonable so we aren't calculating manually.

ericleich avatar Feb 04 '25 23:02 ericleich

It's wrong because I've noticed it on my own app, when I wanted to use the same but with both width&height, using AdSize.getInlineAdaptiveBannerAdSize.

When I've provided it a height of 250dp on my Pixel 6, the conversion from px to dp lost 1 dp, and so it became 249 dp, and then on real ads there were no available ads or there were much smaller ads (about 1/4 of the size I provide) because the closest banner ads are of 250dp. Sadly it seems banner ads aren't flexible in size, despite various functions that are supposed to help with it. It seems there are just multiple fixed sizes, and the functions pick the best one according to the restrictions. This means that for some restrictions, you won't get any ad at all, or much worse than what you provide. In my case, I actually provided a good height and width (full width), and still found this issue that the samples show, which is wrong.

So, the "off-by-1 error" is very critical for banner ads, sadly. And you need to check it with real ads, too, as the test ads are misleading as if everything is ok. Even Admob itself fails to get me ads (or gets me much smaller ads) when the calculations gets me 249dp instead of 250dp

Please re-consider and test for yourself.

AndroidDeveloperLB avatar Feb 05 '25 00:02 AndroidDeveloperLB