Search code examples
androidandroid-recyclerviewadsandroid-relativelayout

Ad body visible even when ad is not loaded


I have implemented a banner ad for my app. When the ad is loaded, recycler view stays up and does not overlaps it nor ad overlaps the recycler view. However, when the ad is not loaded, there still exists a white 'patch' having dimensions same as banner. What I want is that this patch should not exist in the layout if ad is not loaded, and if ad is loaded, create a patch like it and do not over lap recycler view contents. I know there is a frame layout for overlapping different views but app as far as I see needs relative layout to work properly.

XML FILE

<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fafafa"
tools:context=".Activity.MainActivity">

<include
    android:id="@+id/toolbarID"
    layout="@layout/toolbar" />


<TextView
    android:id="@+id/showWhenEmptyID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Add note"
    android:textSize="22sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_below="@id/toolbarID"
    android:layout_marginEnd="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_above="@id/adView"
    android:layout_marginStart="5dp"
    android:layout_marginTop="5dp"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

enter image description here


Solution

  • Try using VISIBILITY modes on adView in your java code based on response from loading of ads.

    Or

    Just set the visibility to GONE in xml and change it to VISIBLE based on response from loading of ads in your java code.

    In your xml:

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:visibility="gone" //SET VISIBILITY GONE
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
    

    And in your activity:

    mAdView = findViewById(R.id.adView)
        val adRequest = AdRequest.Builder().build()
        mAdView.loadAd(adRequest)
        mAdView.adListener = object : AdListener() { //ADDED AD LISTENER
            override fun onAdLoaded() {
                super.onAdLoaded()
    
                adView.visibility = View.VISIBLE
            }
        }