Home > database >  How can I give the recyclerview dynamically constraint?
How can I give the recyclerview dynamically constraint?

Time:06-02

I have layout with a recyclerview and adView. If the ad loads, it looks fine, but if it doesn't, there's a huge gap. If the ad doesn't load, the recyclerview won't have gap, how can I do it?

Layout:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".fragments.listEvent.ListEventFragment">

    <com.google.android.gms.ads.AdView
        android:id="@ id/adView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="4dp"
        app:adSize="SMART_BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/adView" />
</androidx.constraintlayout.widget.ConstraintLayout>

If the ad loads

If the ad doesnt load

CodePudding user response:

You can check if the Ad loading failed, set the Visibility to gone

override fun onAdFailedToLoad(adError: LoadAdError) {
    adView.visibility = View.GONE
}

Or you can by default set the visibility to gone in the XML file. After loading the ad successfully you can make it visible.

In the XML:

android:visibility="gone"

In the code:

adView.visibility = View.VISIBLE

CodePudding user response:

You need a listener to know whether Ad is loaded or not, if Ad is not loaded then hide Ad view, Refer this link.

Stock Overflow Answer

  • Related