Declaratively display ads to the bottom of screen in Android activity without overlapping main content?

StackOverflow https://stackoverflow.com/questions/17771455

Question

In my activity the adview displays at the bottom of the screen but it overlaps my main content instead of resizing it. How to accomplish this?

I followed this guide.

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/contentsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>


    <LinearLayout
        android:id="@+id/adLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/contentsLayout"
        android:layout_alignParentBottom="true"
        android:gravity="bottom|center"
        android:orientation="horizontal" >
    </LinearLayout>

</RelativeLayout>
Was it helpful?

Solution

tell your content layout to be above the adView. Also you will have to reorder the XML like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/adLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="bottom|center"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/contentsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_above="@id/adLayout"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>

</RelativeLayout>

with android:layout_above="@id\adLayout" the content will never get under the adLayout. You need to reorder the xml because the id of the adLayout must be defined to be used with the layout_above

also remove android:layout_alignBottom="@+id/contentsLayout" from the adLayout

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top