Search code examples
androidandroid-layoutandroid-relativelayoutandroid-layoutparams

How to put a AdMob banner below a view with MATCH_PARENT height?


I have a ViewPager with a PagerTitleStrip which has MATCH_PARENT height. The pages has only some text, but some pages have vertical scroll because the text is very long.

Now I'm trying to find the best strategy to put an AdMob banner in the bottom of my layout. This is the situation:

  • If I put below the ViewPager, the banner is not visible, because the ViewPager has MATCH_PARENT.
  • If I put in the bottom of the layout, then, the ViewPager is scrolling content from behind the banner, and this is not a correct behaviour.

It is possible to put a rule which makes the banner to stay below the ViewPager but without making scrolling content of the ViewPager to be from behind the banner?

The way I'm trying to add the banner:

RelativeLayout.LayoutParams adsParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        adsParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adsParams.addRule(RelativeLayout.BELOW,R.id.pager);
        if (AdManager.getInstance().getAdView()!=null){
            View view = AdManager.getInstance().getAdView();
            mainLayout.addView(view, adsParams);
        }

My layout:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <RelativeLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.PagerTitleStrip
                android:id="@+id/pager_title_strip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:paddingTop="4dp"
                android:paddingBottom="4dp"
                style="@style/CustomPagerTitleStrip"/>
        </android.support.v4.view.ViewPager>
    </RelativeLayout>
    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu"
        style="@style/NavigationDrawerStyle"/>
</android.support.v4.widget.DrawerLayout>

Solution

  • In your layout XML res file add this attribute android:layout_above in MATCH_PARENT view. and in its value give the id of ad banner. Now your match_parent view will be above ad banner

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_above="@+id/temp"
            android:layout_height="match_parent"></RelativeLayout>
        <View
            android:id="@+id/temp"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="60dp"/>
    
    </RelativeLayout>