October 21, 2024
Chicago 12, Melborne City, USA
Android

The larger overlay of collapsing banner show on the top of screen


I want to show the larger overlay of collapsing banner on the bottom of screen but it sometimes show on the top of screen.

This is Target result but My result.

My banner ad loading code:

    fun loadAd(
        activity: Activity,
        adViewContainer: FrameLayout,
        listener: (AdView?) -> Unit,
    ) {
        adView = AdView(activity)
        adView?.adUnitId = AdKeys.BANNER
        adSize = activity.let {
            val displayMetrics = it.resources.displayMetrics
            val adWidthPixels =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    val windowMetrics: WindowMetrics = it.windowManager.currentWindowMetrics
                    windowMetrics.bounds.width()
                } else {
                    displayMetrics.widthPixels
                }
            val density = displayMetrics.density
            val adWidth = (adWidthPixels / density).toInt()
            AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(it, adWidth)
        }
        adView?.setAdSize(adSize)
        adViewContainer.addView(adView)
        val extras = Bundle()
        if (isCollapse) {
            extras.putString("collapsible", "bottom")
            extras.putString("collapsible_request_id", UUID.randomUUID().toString())
        }

        // Create an ad request.
        val adRequest =
            AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build()

        adView?.adListener = object : AdListener() {
            override fun onAdOpened() {
                super.onAdOpened()
                isCollapse = false
            }
        }
        // Start loading the ad in the background.
        adView?.loadAd(adRequest)
        listener(adView)
    }

and my xml file:

<?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"
    tools:context=".ui.dashboard.DashBoardFragment">

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:liftOnScrollColor="@null">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/topAppBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:minHeight="?attr/actionBarSize"
                app:menu="@menu/dash_board_top_bar_menu"
                app:navigationIcon="@null">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center">

                    <TextView
                        style="@style/TextAppearance.Material3.TitleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/app_name"
                        android:textFontWeight="700"
                        android:textSize="23sp"
                        tools:targetApi="p" />
                </LinearLayout>
            </com.google.android.material.appbar.MaterialToolbar>
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/categories_recycle_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_ad_banner"
            android:elevation="3dp"
            android:paddingTop="3dp">

            <FrameLayout
                android:id="@+id/bannerAdDashBoard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom" />
        </FrameLayout>
    </LinearLayout>
</layout>

I read Why my Admob banner is showing on top of screen? and tried to add android:layout_rabity="bottom" to the layout but it didn’t work.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video