Search code examples
androiduser-interfacealignmentandroid-relativelayout

alignParentBottom appears Offscreen in Fragment with Scroll View


For those wanting to have an adView in their app that is comprised of Fragment layouts, hope the below helps you!

Basically, I was originally trying to place the adView inside the Layout XML for each fragment. This caused the adView to either be pushed off the screen or not play nice with the relative layout commands (ex. alignParentBottom).

The solution was to move the adView into the Main Activity Layout outside of the the Coordinator Layout used for my fragments. Then I wrapped the Coordinator Layout and adView in a Relative Layout.

This then allowed me to fully control the adView and present on each fragment pinned to the bottom of the screen.


Solution

  • I have found that when I change the background of your ScrollView, it paints OVER the AdView. You MUST place your AdView below the ScrollView in code. That way, it is drawn AFTER the ScrollView and is therefore on top.

    Here is your edited code:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">
    
        <!-- Dummy item to prevent EditTextView from receiving focus -->
        <LinearLayout
            android:id="@+id/dummyLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="horizontal" />
        <!-- Dummy item to prevent EditTextView from receiving focus -->
    
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="4"
                android:hint="@string/hint"
                android:nextFocusLeft="@id/editText1"
                android:nextFocusUp="@id/editText1"
                android:singleLine="true"/>
    
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/linearLayout1"/>
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView4"/>
    
        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/button1"
            android:background="@color/colorAccent0"
            android:stretchColumns="*"/>
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tableLayout1"
            android:layout_above="@+id/adView"
            android:background="@color/colorGray">
    
            <TableLayout
                android:id="@+id/tableLayout2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:stretchColumns="*"/>
        </ScrollView>
    
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>
    
    </RelativeLayout>