sirswistak
sirswistak

Reputation: 59

Admob in the bottom of RecyclerView and NestedScrollView

I have RecyclerView with NestedScrollView and Toolbar. I want to place adview banner in the bottom so when a user is scrolling the banner is always visible. I don't know how to separate these views because nestedscrollview is not working when I changed it to LinearLayout. My code looks like this:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".BenefitsActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_benefits"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_width="match_parent"
            android:background="#109689"
            app:title="@string/wlasciwosci"
            android:layout_height="?attr/actionBarSize">

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="true"
        android:measureAllChildren="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="50dp"
            android:clipToPadding="false"
            android:background="@color/grey_background"
            android:id="@+id/benefits_recycler"/>

    </androidx.core.widget.NestedScrollView>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Upvotes: 2

Views: 441

Answers (2)

Attaullah
Attaullah

Reputation: 4021

Enclose your layout in LinearLayout and add wight 10 and height 0dp

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="10"
    android:orientation="vertical">

    <androidx.coordinatorlayout.widget.CoordinatorLayout  
      android:layout_width="match_parent"
      android:layout_weight="10" <----- add this
      android:layout_height="0dp" <----- add this
      tools:context=".BenefitsActivity">
        ....
        ....
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    <com.google.android.gms.ads.AdView <----- add banner here
        android:id="@+id/abb_adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:visibility="gone"
        app:adSize="SMART_BANNER"
        app:adUnitId="@string/bannerAd" />
</LinearLayout>

Upvotes: 1

Nour Eldien Mohamed
Nour Eldien Mohamed

Reputation: 961

try make height of NestedScrollView and RecyclerView wrap_content not match_parent

  <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:isScrollContainer="true"
        android:measureAllChildren="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="50dp"
            android:clipToPadding="false"
            android:background="@color/grey_background"
            android:id="@+id/benefits_recycler"/>

    </androidx.core.widget.NestedScrollView>

Upvotes: 1

Related Questions