diff --git a/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt b/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt index fd94ff1ff..1c3590a4b 100644 --- a/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt +++ b/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt @@ -85,7 +85,7 @@ internal class AdManagerImpl(context: Context) : AdManager { override fun getBannerAd( context: Context, adId: String, - maxHeight: Int + maxHeightInDp: Float ): BannerAdView { Logger.v { "AdManagerImpl getBannerAd" } @@ -96,13 +96,15 @@ internal class AdManagerImpl(context: Context) : AdManager { context.resources.displayMetrics.widthPixels } + val adHeight = maxHeightInDp.toInt().toDp(context) + val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize( context, - (adWidthPixels / context.resources.displayMetrics.density).toInt() + adWidthPixels.toDp(context) ) - val finalAdSize = if (adSize.height > maxHeight) { - AdSize(adSize.width, maxHeight) + val finalAdSize = if (adSize.height > adHeight) { + AdSize(adSize.width, adHeight) } else { adSize } @@ -177,6 +179,9 @@ internal class AdManagerImpl(context: Context) : AdManager { ) } + private fun Int.toDp(context: Context) = + (this / context.resources.displayMetrics.density).toInt() + private fun Activity.initializeMobileAdsSdk() { Logger.v { "AdManagerImpl initializeMobileAdsSdk" } diff --git a/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt b/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt index d5d7a3522..3ed6f0316 100644 --- a/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt +++ b/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt @@ -38,7 +38,7 @@ internal class AdManagerImpl : AdManager { override fun getBannerAd( context: Context, adId: String, - maxHeight: Int + maxHeightInDp: Float ): BannerAdView { Logger.v { "AdManagerImpl getBannerAd" } diff --git a/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt b/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt index 12fc1ad67..9f9b4e1bf 100644 --- a/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt +++ b/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt @@ -14,7 +14,7 @@ interface AdManager { fun getBannerAd( context: Context, adId: String, - maxHeight: Int + maxHeightInDp: Float ): BannerAdView fun showInterstitialAd( diff --git a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt index cd0cdfaa3..f0d841543 100644 --- a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt +++ b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt @@ -33,10 +33,14 @@ fun FrameLayout.buildBanner( adId: String, shouldShowAd: Boolean ) = if (shouldShowAd) { - val maxAdHeight = - (context.resources.getDimension(R.dimen.ads_banner_height) / resources.displayMetrics.density).toInt() removeAllViews() - addView(adManager.getBannerAd(context, adId, maxAdHeight)) + addView( + adManager.getBannerAd( + context, + adId, + context.resources.getDimension(R.dimen.ads_banner_height) + ) + ) } else { visibility = View.GONE }