Search code examples
xmlandroid-layoutadview

How to push AdView to Bottom of Page in Android App


how can I push AdView (AdMob) ads to the bottom of page in Android Activity? here's the code for my activity_main.xml layout. for some reason it does not align to bottom. I've tried putting the AdView inside couldn't get working. Any help appreciated.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout_main"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <!--custome toolbar-->
    <include layout="@layout/tool_bar" />

    <!--Wifi name and state-->
    <LinearLayout
        android:id="@+id/linear_wifi_name"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/wifi_icon_id"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.20" />

        <TextView
            android:id="@+id/wifi_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.60"
            android:textSize="20dp"
            android:gravity="center"
            android:textAlignment="center" />

        <ImageView
            android:id="@+id/scan_icon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.20"
            android:src="@drawable/ic_keyboard_arrow_right_white_36dp"/>

    </LinearLayout>

    <!--Progess bar-->
    <ProgressBar
        style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:id="@+id/progress_bar" />

    <!--this section is for wifi/private network-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/result_local"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:text="Local Network:"
            android:textColor="@color/colorAccent"
            android:textSize="20dp"/>

        <TextView
            android:id="@+id/private_net_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:textSize="16dp"/>

    </LinearLayout>

    <!--this section is for public ip info-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/result_public"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:textColor="@color/colorAccent"
            android:text="Public Ip:"
            android:textSize="20dp"/>

        <TextView
            android:id="@+id/public_net_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:textSize="16dp"/>

    </LinearLayout>

    <!-- banner ads for AdsMob-->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:padding="5dp">
    </com.google.android.gms.ads.AdView>

</LinearLayout>

Solution

  • One of the other views has to fill in the remaining space so the ad is pushed to the bottom. You can accomplish that by adding this right above the AdView:

    <!-- fill remaining space -->
    <Space
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />
    

    Also note: weights don't have to add up to 1. It's only relative to the other children. So where you used the weights 0.2, 0.6 and 0.2 in your layout you could also use 1, 3 and 1 for the same effect.
    If only one child has a weight it consumes all remaining space no matter the value.