Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView adds large space at the bottom

My main Activity has a number of buttons, that fit below each other on a large Tablet screen, but not on a smaller Phone screen. I added a ScrollView, so that the user can scroll down to the other buttons if the screen size requires it:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:background="@drawable/bg"
          android:gravity="center_horizontal">

    <TextView
        android:id="@+id/trackSelectorText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

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

        <Button                    
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="foo" />
        <!-- More buttons -->
        </LinearLayout>
    </ScrollView>
</LinearLayout>

This kinda works, however I have a large blank space below the last button (i.e. the ScrollView can scroll down too far). Both on an actual Tablet and on the emulator with a Phone configuration. If I center the inner LinearLayout (instead of center_horizontal), the space is evenly spread between the top and the bottom, which I don't want neither. How do I fix this layout?

I hope this image makes it more clear, imagine that the ScrollView has been completely scrolled down and showing the last buttons. The blank space I mean is the one below Button 6 on the right image:

enter image description here

like image 271
Lennart Avatar asked Sep 06 '13 13:09

Lennart


People also ask

What is fillViewport in ScrollView?

fillViewport allows scrollView to extend it's height equals to the full height of device screen's height in the cases when the child of scroll view has less height.

Is ScrollView a ViewGroup?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.

Is CoordinatorLayout scrollable?

Android Layouts CoordinatorLayout Scrolling BehaviorAn enclosing CoordinatorLayout can be used to achieve Material Design Scrolling Effects when using inner layouts that support Nested Scrolling, such as NestedScrollView or RecyclerView .


1 Answers

Try adding this attribute to your ScrollView element. android:fillViewport="true"

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:fillViewport="true" > ... </ScrollVIew>

Ref: ScrollView Ref

like image 114
Ezra Avatar answered Sep 30 '22 17:09

Ezra