Solution 1 :
Nice … The whole idea is that scroll view takes only one direct child … If you need the layout to be in it, add linear layout to the scroll view. Now you can include the two layout in the linear layout…
Problem :
first question here, so apologies in advance if I forget to add anything or mess up formatting. I have a fragment that I would like to put a ScrollView on, and inside of that ScrollView I want to be able to scroll between two different XML layouts I’ve created. I am using <include> to add these layouts to a LinearLayout, and then putting that LinearLayout inside of the ScrollView. However, this doesn’t seem to work at all, and all I see is the first <include> layout and I am not able to scroll vertically to view the second layout. However, when I test out the ScrollView with a LinearLayout containing simple ImageViews and TextViews, it seems to work just fine. I’m just confused on the proper way to use ScrollView with multiple XML layouts.
Here is a layout that I am including:
<?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"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_background="#80000000"
android_alpha=".8"
android_orientation="vertical"
android_scrollbars="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android_layout_width="match_parent"
android_layout_height="match_parent">
<ImageView
android_layout_width="0dp"
android_layout_height="0dp"
android_layout_marginStart="190dp"
android_layout_marginLeft="190dp"
android_layout_marginTop="20dp"
android_layout_marginEnd="10dp"
android_layout_marginRight="10dp"
android_layout_marginBottom="300dp"
android_adjustViewBounds="false"
android_cropToPadding="false"
android_scaleType="centerCrop"
android_src="@drawable/originium_shard"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.5"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
android_scrollbars="vertical">
</ImageView>
<TextView
android_id="@+id/txtOriginium"
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_marginTop="20dp"
android_layout_marginEnd="200dp"
android_layout_marginRight="200dp"
android_fontFamily="@font/average_sans"
android_padding="10dp"
android_shadowColor="@color/colorBlack"
android_shadowDx="8"
android_shadowDy="8"
android_shadowRadius="1"
android_text="A deadly disease."
android_textColor="@color/colorWhite"
android_textSize="50dp"
android_typeface="normal"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
android_scrollbars="vertical"/>
<ImageView
android_id="@+id/redrectWorld1"
android_layout_width="273dp"
android_layout_height="39dp"
android_scaleType="centerCrop"
app_layout_constraintBottom_toBottomOf="@+id/breakingNewsWorld1"
app_layout_constraintEnd_toEndOf="@+id/breakingNewsWorld1"
app_layout_constraintHorizontal_bias="0.0"
app_layout_constraintStart_toStartOf="@+id/breakingNewsWorld1"
app_layout_constraintTop_toTopOf="@+id/breakingNewsWorld1"
app_layout_constraintVertical_bias="1.0"
app_srcCompat="@drawable/redrect"
android_scrollbars="vertical"></ImageView>
<TextView
android_id="@+id/breakingNewsWorld1"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginStart="10dp"
android_layout_marginLeft="10dp"
android_fontFamily="@font/press_start_2p"
android_paddingLeft="6dp"
android_paddingBottom="6dp"
android_text="BREAKING NEWS"
android_textColor="@color/colorWhite"
android_textSize="20sp"
android_typeface="normal"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintVertical_bias="0.519"
android_scrollbars="vertical"></TextView>
<ImageView
android_id="@+id/blackrectWorld1"
android_layout_width="0dp"
android_layout_height="0dp"
android_layout_marginEnd="10dp"
android_layout_marginRight="10dp"
android_layout_marginBottom="20dp"
android_alpha=".7"
android_scaleType="centerCrop"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="@+id/redrectWorld1"
app_layout_constraintTop_toBottomOf="@+id/redrectWorld1"
app_srcCompat="@drawable/blackrect"
android_scrollbars="vertical"></ImageView>
<TextView
android_id="@+id/introblurb"
android_layout_width="0dp"
android_layout_height="0dp"
android_fontFamily="sans-serif-condensed"
android_padding="6dp"
android_text="@string/intro_blurb"
android_textColor="@color/colorWhite"
android_textSize="16sp"
app_layout_constraintBottom_toBottomOf="@+id/blackrectWorld1"
app_layout_constraintEnd_toEndOf="@+id/blackrectWorld1"
app_layout_constraintStart_toStartOf="@+id/blackrectWorld1"
app_layout_constraintTop_toTopOf="@+id/blackrectWorld1"
android_scrollbars="vertical"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
The other layout that I want to scroll to is almost the same, so I will omit the code. Here is my fragment’s code:
<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/originium_bg"
tools_context=".Landing2Fragment"
android_orientation="vertical"
android_scrollbars="vertical"
android_fillViewport="true">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical"
android_scrollbars="vertical">
<include
layout="@layout/world_1">
</include>
<include
layout="@layout/world_2">
</include>
</LinearLayout>
</ScrollView>
Here is an image of what the emulator looks like.
It is stuck on this first layout and I cannot scroll at all. Any help with this issue would be appreciated.
EDIT: So I was still unable to find the solution to my original problem, but I was able to awkwardly work around it. I think it has something to do with ConstraintLayout being within a LinearLayout, because when I recreated my layout pages using RelativeLayout instead of ConstraintLayout that I originally had, using <include> works and I can scroll properly. Still not sure what the exact issue is, but I believe it has something to do with that ConstraintLayout and LinearLayout interaction.