Reputation: 3275
I'm new to android development and I have the following problem.
I need to use FrameLayout
inside of ScrollView
for making it scrollable. I wrote this
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#00ff00">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#ff0000">
</FrameLayout>
</ScrollView>
But this doesn't work. I tried in RelativeLayout
and it worked, but I need to use for FrameLayout
.
Any ideas?
Upvotes: 11
Views: 22883
Reputation: 2468
android:fillViewport="true" solves this issue.
android:fillViewport, Defines whether the scrollview should stretch its content to fill the viewport.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</ScrollView>
Upvotes: 24
Reputation: 8929
To get FrameLayout inside ScrollView, do this:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayoutHeader1"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="1440dp"
>
</FrameLayout>
</LinearLayout>
</ScrollView>
Upvotes: 3
Reputation: 3275
I tried many variants for solving this problem, including answers here, but no one was helpful. ScrollView
always wraps FrameLayout
, but everything is normal with RelativeLayout
, LinearLayout
...
I have found one solution for this - to set minimum height of FrameLayout
. You can set dynamically minimum height by setMinimumHeight()
method.
Upvotes: 6
Reputation: 17105
Try setting layout_height to "wrap_content" in your FrameLayout
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000">
It will scroll when the FrameLayout will be higher than the screen border. Or if you want the fixed height of scrollable area, set height to 500dp on ScrollView. It depends what you want, but don't ever set the fixed height on a ScrollView child. The ScrollView child's height should always be wrap_content.
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#00ff00">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000">
</FrameLayout>
</ScrollView>
Upvotes: 2
Reputation: 4689
Your xml sample is quite good, the only thing I can see is : if the ScrollLayout parent is bigger than 500dp it won't scroll.
Indeed, the scroll is used only if the content is bigger than the scrollview.
here, you set your content height to 500dip and you scrollview to 'match_parent' if the parent of this scrollView take the whole screen, on a 800dip height screen (for example)
=> the scroll is simply not needed.
Upvotes: 2