I published my app with AdMob Banner since 2018. On 2020, I changed the banner into Adaptive Banner. On my latest release, I am having an issue with it.
The Adaptive Banner takeover screen with wrong height
As shown above, the Adaptive Banner takeover most of the screen with abnormal height for a banner.
Here's my Admob:
// ADMOB
implementation "com.google.android.gms:play-services-ads:20.2.0"
My MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/ad_view_container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<FrameLayout
android:id="@+id/ad_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
My MainActivity.kt
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private var adView: AdView? = null
private var adRequest: AdRequest? = null
private var initialLayoutComplete = false
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = adViewContainer.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
override fun onCreate(savedInstanceState: Bundle?) {
...
admobSetup()
...
}
private fun adMobSetup() {
if (adViewContainer.visibility == View.GONE)
adViewContainer.visibility = View.VISIBLE
adRequest = setAdRequest()
MobileAds.initialize(this) { }
initAdViewAndLoadBanner()
}
private fun initAdViewAndLoadBanner() {
adView = AdView(this)
adViewContainer.removeAllViews()
adViewContainer.addView(adView)
adViewContainer.viewTreeObserver.addOnGlobalLayoutListener {
if (!initialLayoutComplete) {
initialLayoutComplete = true
loadBanner()
}
}
}
private fun loadBanner() {
adView?.adUnitId = getString(R.string.AD_UNIT_ID)
adView?.adSize = adSize
// Start loading the ad in the background.
adView?.loadAd(adRequest!!)
}
}
By the way, I am using DataBinding, not sure if their is an issue linked to it. Any solution for this?