Search code examples
androidadmobandroid-linearlayoutandroid-scrollviewadview

AdView doesn't show ads at the bottom of the linear layout layout -Why So?


I'm trying to show admob adview at the bottom of the screen but it doesn't work. It shows ad below the text if the text is short (doesn't need to scroll) , but if the text is long (need to scroll) the adview doesn't show at all. I want to show the adview at the bottom of my screen no matter how long or short the text is. I have been at this for hours but couldn't figure out to make it work.

Here is my Activity.xml

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@color/backgroundColor"
    tools:context=".activities.DetailActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:elevation="1.46dp"
        android:background="@color/backgroundColor">

        <TextView
            android:id="@+id/tvWordDetail"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="@color/textColorPrimary"
            android:textSize="20sp"
            android:gravity="center_vertical"/>

        <ImageButton
            android:id="@+id/textToSpeech"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:contentDescription="@string/text_to_speech"
            android:background="?actionBarItemBackground"
            app:srcCompat="@drawable/ic_volume"/>

        <ImageButton
            android:id="@+id/btnBookmark"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:contentDescription="@string/add_bookmark"
            android:background="?actionBarItemBackground"
            app:srcCompat="@drawable/ic_bookmark_border"/>

    </LinearLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/viewColor"/>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/backgroundColor"
            android:elevation="1.46dp"
            android:orientation="vertical"
            android:paddingLeft="16dp"
            android:paddingTop="10dp"
            android:paddingRight="16dp">


            <TextView
                android:id="@+id/tvWordDefinition"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textColor="@color/textColorSecondary"
                android:textSize="18sp" />

        </LinearLayout>

    </ScrollView>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adViewDetail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_banner_ad_detail">

    </com.google.android.gms.ads.AdView>

</LinearLayout>

Solution

  • Add one new attributes android:layout_weight="1" and Modify height of scroll view as below.

        android:layout_height="0dp"
        android:layout_weight="1"
    

    So final attributes set of scrollview looks like,

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">