Bart Friederichs
Bart Friederichs

Reputation: 33501

ScrollView and LinearLayout

I am trying to get a quite simple layout in Android, but I just can't get it to work. All I want is a header (by include an XML file), followed by a ScrollView to show a large body of text, and two buttons in the bottom, that should always be visible.

I have fiddled with LinearLayouts and RelativeLayouts, but somehow, I can't get it to work. What I have until now is this:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <include layout="@layout/header" />


    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/svDisclaimer"
        >
           <TextView 
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" 
               android:id="@+id/tvDisclaimer">
           </TextView>
    </ScrollView>
         <LinearLayout      android:orientation="horizontal" 
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:layout_below="@+id/svDisclaimer"
               >
      .. [ snip ] ..
        </LinearLayout>
        </RelativeLayout>
</LinearLayout>

( .. [snip] .. is where my buttons are) The header is there, the scrollview pops up, but the buttons are nowhere to be seen.

Upvotes: 3

Views: 894

Answers (3)

AC Arcana
AC Arcana

Reputation: 366

Another alternative is to put your top and bottom elements that must stay on the screen in framelayouts. http://android-er.blogspot.ca/2011/06/example-of-framelayout.html

Upvotes: 0

Sam
Sam

Reputation: 86948

Your ScrollView's height is set to fill_parent which pushes everything below the ScrollView off the bottom of the screen. You will never see what you cannot scroll to... try this pattern instead:

<ScrollView >
    <RelativeLayout >
        <TextView />
        <LinearLayout >
            <!-- [ snip ] -->
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

You can probably remove the LinearLayout by using the RelativeLayout position tags as well. (android:below, android:toRightOf, etc)


Try using this configuration from my comment:

<ScrollView
    ...
    android:above="@+id/buttons" >

    ...
</ScrollView>
<LinearLayout
    android:id="@+id/buttons"
    android:layout_alignParentBottom="true"
    ... >

    ...
</LinearLayout>

Upvotes: 4

Kevin
Kevin

Reputation: 15934

Like Sam said, setting the ScrollView's height to fill_parent pushes the buttons off the screen. There's a way to make it work using the layout tags for RelativeLayout, though. Try this:

<LinearLayout>
[snip]
    <RelativeLayout>
         <!-- Declare the buttons *before* the ScrollView, but use layout params to move
         them to the bottom of the screen -->
         <LinearLayout
            android:id="@+id/buttonParent"
            android:layout_alignParentBottom="true">
              [Buttons go here]
         </LinearLayout>
         <!-- The "android:layout_above" attribute forces the ScrollView to leave space for the buttons -->
         <ScrollView
           android:height="fill_parent"
           android:layout_above="@id/buttonParent" />
             [snip]
         </ScrollView>
    </RelativeLayout>
</LinearLayout>

Upvotes: 0

Related Questions