Search code examples
androidadmobfragmentbanner

Placing banner ad on bottom of parent with fragments Android


I'm making an android app but having trouble with positioning a banner.

I have an Activity acting as a container for fragments. I basically want a Scroll View for the root, then the fragment, and an ad on the very bottom.

Here is my XML.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"></LinearLayout>

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:layout_constraintBottom_toBottomOf="parent"
            ads:layout_constraintStart_toStartOf="parent"
            ads:layout_constraintEnd_toEndOf="parent"
            ads:layout_constraintTop_toBottomOf="@id/fragment_container"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-xxxx"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Here's how the layout looks like

enter image description here

Basically, the ad is placed directly below the fragment container and it isn't attaching to the viewport's bottom.

I want it to attach to the bottom, then have the scroll view scroll both the fragment + ad together.

This is the only way I could get the ad to appear on the bottom (in short terms):

<Constraint layout>
       <Scroll View>
          <Fragment container>
<Ad constraining to parent bottom>

However, with this method, the scroll view would only scroll the fragment and doesn't include the ad, meaning you'll get the ad overlapping the fragment content sometimes.

So I think the scroll view must be the root view. However, I'm having trouble managing the child to have the ad appear on the scroll view's bottom, instead of the children's bottom edge.


Solution

  • Greets by Nice! Can you try it with RelativeLayout ;)

    <?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:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                    <LinearLayout
                        android:id="@+id/fragment_container"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" />
    
    
            </ScrollView>
    
            <com.google.android.gms.ads.AdView
                android:id="@+id/adView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adSize="BANNER"
                ads:adUnitId="ca-app-pub-xxxx"
                android:layout_alignParentBottom="true"/> <!--just add this line-->
    
        </RelativeLayout>