Home > Enterprise >  How do I add adView outside of scrollview in android studio?
How do I add adView outside of scrollview in android studio?

Time:10-05

I am trying to add a banner add at the bottom of my app but I don't know how to put it so that it is not affected by the scrollview. I want it to sit at the bottom of my screen without it being affected by the scroll effect how do I do this, when I try putting the AdView code outside the ScrollView it doesn't work. This is how my activity_main.xml looks like

<ScrollView 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
tools:context=".MainActivity">

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

    <androidx.cardview.widget.CardView
        android:layout_margin="16dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="8dp"
        app:cardBackgroundColor="@color/blue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.SearchView
            android:id="@ id/search_view"
            app:defaultQueryHint="Search..."
            app:iconifiedByDefault="false"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>
    </androidx.cardview.widget.CardView>

        <LinearLayout
        android:layout_margin="16dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <TextView
            android:id="@ id/textView_a"
            android:textSize="24sp"
            android:text="A"
            android:textColor="@color/blue"
            android:textAlignment="center"
            android:padding="8dp"
            android:textStyle="italic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
            <androidx.recyclerview.widget.RecyclerView
            android:id="@ id/recycler_a"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
            <TextView
            android:textSize="24sp"
            android:text="B"
            android:textColor="@color/blue"
            android:textAlignment="center"
            android:padding="8dp"
            android:textStyle="italic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
            <androidx.recyclerview.widget.RecyclerView
            android:id="@ id/recycler_b"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@ id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>
    </LinearLayout>
</LinearLayout>

CodePudding user response:

This is my suggestion: Put scrollview inside constrainLayout, like this:

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

     <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@ id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" ...>

        //main layout here

        </LinearLayout>
    </ScrollView>

    <AdView
        android:id="@ id/view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Make your parent layout to LinearLayout, add the AdView to be on the top (width to match the parent and the height to wrap content), add your scrollView (width and height to match parent)

The xml will look like this

<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <AdView
        android:id="@ id/view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toTopOf="@ id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        // your layout.

    </ScrollView>
</LinearLayout>
  • Related