5

I am trying to keep the ScrollView from taking too much space at the bottom of the screen as it keeps my two buttons from showing. I also don't want to manually set a height for the ScrollView.

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   <LinearLayout
      ...
      android:orientation="horizontal">
        <Button .../>
        <TextView .../>
        <Button .../>
   </LinearLayout>
   <ScrollView
      android:layout_width="math_parent"
      android:layout_height="wrap_content">
      <ImageView .../>
      <ImageView .../>
      <ImageView .../>
   </ScrollView>
   <LinearLayout
      ...
      android:orientation="horizontal">
        <Button .../>
        <Button .../>
   </LinearLayout>
</LinearLayout>

This is a generalized version of my layout. What is happening is that the ScrollView extends all the way to the bottom of the screen no matter how many items are in it. This causes the two buttons at the bottom of the screen to not be visible.

How can I stop this from happening without manually setting a height for the ScrollView?

I've used android:layout_height="wrap_content" for all my views.

Isn't this supposed to automatically distribute the height of the views to fit the screen's height?

(Wanted to include images, but my rep isn't high enough yet (-_-))

rene
  • 41,474
  • 78
  • 114
  • 152
edc598
  • 317
  • 3
  • 11
  • 1
    It might be a good idea to switch to RelativeLayout as your root layout. It should give you tools to have ScrollView kept within the center area of the view without explicitly calculating its height yourself. – harism Dec 30 '11 at 15:06
  • Thanks for the comment. I've since implemented a different solution, but I've learned a couple more things along the way and I believe your implementation would be best suited for what I wanted to do. A RelativeLayout would allow me to anchor the Buttons to the bottom of the screen and then place all my other UI objects in between the Top of the screen and the Buttons. – edc598 Feb 01 '12 at 03:51
  • Your ScrollView should have no more than one child. No idea if this is the cause of your problems though. – Wytze Apr 14 '14 at 14:20

2 Answers2

3

The way I would resolve this today is by using RelativeLayout as a base. Anchor the two LinearLayout's to the top and bottom of the RelLayout respectively. Then I would insert the ScrollView but I would make sure to set it's layout properties as follows:

android:layout_below="topLinearLayout" android:layout_above="bottomLinearLayout"

edc598
  • 317
  • 3
  • 11
  • shouldn't it be "alignParentTop=true" (same for bottom) – ataulm Sep 16 '13 at 20:33
  • Yes for the top LinearLayout you would put `alignParentTop=true` and for the bottom LinearLayout you would have `alignParentBottom=true` but for the ScrollView the way I did it is as indicated above, in other words make the top edge of the ScrollView anchor to the bottom edge of the top LinearLayout, bottom edge to the top edge of the bottom LinearLayout. – edc598 Sep 16 '13 at 21:02
2

Have you tried using layout_weight for the ScrollView. IIRC, if a non-zero weight is set for ONE element (and the corresponding dimension set to 0dp) then it'll fill up the remaining space in that dimension, after setting out the measurements for sibling views (in this case, the LinearLayout on top and bottom of the ScrollView.

Apologies, not got an Android environment to check and it's been a while.

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

   <LinearLayout>
      ...
      Something at the top
      ...
   </LinearLayout>

   <ScrollView
      android:layout_width="match_parent"
      **android:layout_height="0dp"
      android:layout_weight="1"**>
      <ImageView .../>
      <ImageView .../>
      <ImageView .../>
   </ScrollView>

   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" >
      ...
      buttons
      ...
   </LinearLayout>
</LinearLayout>

Here's an answer with more information about the layout_weight attribute.

Community
  • 1
  • 1
ataulm
  • 15,195
  • 7
  • 50
  • 92