How to put admob banner to bottom of the screen using CoordinatorLayout

跟風遠走 提交于 2019-12-03 16:16:06

try below mechanism.Put your viewpager and AdView inside RelativeLayout.

here is the sample layout file.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?actionBarSize">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager"
       >

    </android.support.v4.view.ViewPager>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">

    </com.google.android.gms.ads.AdView>
</RelativeLayout>

I used @patel's answer but it makes my Tablayout to overlap with the Viewpager's content; The fix I used is to just put the adview below the ViewPager and add

android:layout_gravity="bottom|center"  

Working Code:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_marginBottom="@dimen/dp_56"/>

<com.google.android.gms.ads.AdView
    android:id="@+id/banner_ad_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_56"
    android:visibility="gone"
    ads:adSize="BANNER"
    app:adUnitId="@string/ad_banner_size_id"
    android:layout_gravity="bottom|center"/>

Result:

<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom|end"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />
    <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="SMART_BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">

</com.google.android.gms.ads.AdView>

//try this comment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!