I'm developing an app in which some data is getting loaded from Firebase and is shown in the RecyclerView.
What I want is I want to show the AdView below the RecyclerView and for that I have done this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/recyclerView_parentLayout"
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="8dp"
android:paddingRight="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.abc.xyz.MainActivity"
tools:showIn="@layout/app_bar_main"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.v7.widget.CardView
android:id="@+id/card_ads"
android:layout_below="@id/recycler_view"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentPaddingBottom="2dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true" >
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
ads:adUnitId="ca-app-pub-***********/*********"
ads:adSize="280x80">
</com.google.android.gms.ads.NativeExpressAdView>
</android.support.v7.widget.CardView>
</RelativeLayout>
The problem is that this layout is showing no ads at the bottom and the recyclerview is covering the whole area and the ads are getting covered by it.
Here's the screenshot (the recyclerview has been scrolled to the last and there is no ad there):
Please let me know how can I place the ads just below the RecyclerView or where the recyclerview ends.
You should use LinearLayout instead of RelativeLayout. In LinearLayout put ad at the bottom of the screen and let it occupy it's space and set layout_weight to your RecyclerView.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
... >
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
... />
<android.support.v7.widget.CardView
android:id="@+id/card_ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
... >
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
</android.support.v7.widget.CardView>
</LinearLayout>
And one more thing, don't set layout_height of AdView to match_parent, use wrap_content instead.
Try to put your RecyclerView inside other layout, or just set layout_above="@id/card_ads"
I think the main problem is that you set in card_ads layout_below, but not tell recyclerview that it should be over the card_ads
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView_parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="8dp"
android:paddingRight="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.abc.xyz.MainActivity"
tools:showIn="@layout/app_bar_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="card_ads"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>
<android.support.v7.widget.CardView
android:id="@+id/card_ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/recycler_view"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPaddingBottom="2dp">
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
ads:adSize="280x80"
ads:adUnitId="ca-app-pub-***********/*********">
</com.google.android.gms.ads.NativeExpressAdView>
</android.support.v7.widget.CardView>
</RelativeLayout>
I've add Admob on my App, you can check at Github
Edit:
I put match parent, see if it works
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Add framelayout as a main layout .Try this code .Put your adView insted of button .
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView_parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="8dp"
android:paddingRight="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.v7.widget.CardView
android:id="@+id/card_ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/recycler_view"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPaddingBottom="2dp">
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
ads:adSize="280x80"
ads:adUnitId="ca-app-pub-***********/*********">
</com.google.android.gms.ads.NativeExpressAdView>
</android.support.v7.widget.CardView>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</FrameLayout>
Add this to your XML
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/adView"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
来源:https://stackoverflow.com/questions/37977521/how-to-place-adview-below-the-recyclerview-please-see-details
