Search code examples
androidandroid-layoutandroid-linearlayoutandroid-scrollviewbanner-ads

Layout content after ScrollView Linear Layout not showing


I have a full page ScrollView Linear layout on my MainActivity.xml. Now I want to add Admob Banner Ad layout after ScrollView layout so that my banner stays sticky at bottom. But after ScrollView layout, whatever I do add, nothing displays. if I ad banner ads code inside ScrollView, it works but I need to place it after scrollview layout. I want ScrollView layout_height="wrap_content" because device to device height/width varies. Need someone's help. my xml code is below:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/back2"

    tools:context="com.domain.appname.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="230dp"
                app:srcCompat="@drawable/head" />


            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="App Title"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold"
                android:layout_marginTop="15dp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="App Description"
                android:textSize="20sp"/>

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="15dp"
                android:text="List Page"
                android:layout_marginBottom="15dp"
                android:textSize="20sp"
                style="@style/Widget.AppCompat.Button.Colored"
                android:onClick="switcher"/>
        </LinearLayout>        
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/banner_ad_unit_id"
            >
        </com.google.android.gms.ads.AdView>
    </LinearLayout>
</LinearLayout>

Solution

  • Set the ScrollViews height to 0dp and remove the weight on the AdView (you can also remove its LinearLayout wrapper)

    This works because; the AdView has its own size (wrap_content). The ScrollView has no size, but has a weight, so all remaining space (ie the parents height minus the adviews height) is assigned to ScrollView.