I am trying to limit my ScrollView to only a certain section of my screen as I am trying to attach a banner ad to the bottom of my screen and I want the banner ad to be static while the rest of the contents to be scroll-able therefore I don't want the banner ad to be affected by the ScrollView. For some reason though only the scroll-able section is visible on my device screen when I run my app, my banner ad does not show up at all. I have tried multiple methods to fix this problem to make it work and yet I still can't figure it out. This is my current activity_main.xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="@color/grey"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<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_phonetics"
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_meanings"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent">
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
First of all, never use RecyclerView
inside Scrollview
. Instead, use NestedScrollView
.
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
If you don't want your RecyclerViews
too be scrollable in the first place, add your content as child views to a layout view.
Second, for your ScrollView
to take up all the remaining space given by the AdView
, craft a constraint chain and give the ScrollView
a height of 0dp
. This means, you need to constrain the bottom of the ScrollView
to the top of the AdView
and vice versa.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
...>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/ScrollView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/LinearAdLayout">
<!--Your content here-->
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/LinearAdLayout"
app:layout_constraintTop_toBottomOf="@id/ScrollView"
app:layout_constraintBottom_toBottomOf="parent">
<!--Your AdView here-->
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.