Search code examples
androidandroid-linearlayoutfill

How to make widget in the middle of layout stretch to fill the parent in Android


I have an Android layout question. I have a display where I am putting some text into the middle of a view that has a header at the top and three buttons at the bottom. What I am having trouble with is getting the TextView (which is inside a ScrollView) to stretch to fill up the available screen space. If I set the layout_height of the ScrollView to "fill_parent" the three buttons are pushed off the bottom of the screen. If I set it to "wrap_content" it is only as large as is needed to support the text that is put into the TextView.

Here is the XML for the layout I am using:

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

<LinearLayout android:orientation="horizontal"
    android:gravity="top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher">
    </ImageView>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        style="@android:style/TextAppearance.Large"
        android:text="@string/meetingdetail" />
</LinearLayout>

<ScrollView
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <TextView android:id="@+id/meeting"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        style="@android:style/TextAppearance.Medium" />
</ScrollView>

<Button android:id="@+id/navigation"
    android:text="@string/naviation"
    android:onClick="meetingInfoOnClick"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Button android:id="@+id/mapsingle"
    android:text="@string/mapsingle"
    android:onClick="meetingInfoOnClick"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Button android:id="@+id/goback"
    android:text="@string/goback"
    android:onClick="meetingInfoOnClick"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

Could someone suggest what would work best to make the ScrollView with the TextView fill up the available space but still allow the three buttons to appear?


Solution

  • Set the height of the ScrollView to 0dp and its weight to 1.

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    
    <TextView android:id="@+id/meeting"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        style="@android:style/TextAppearance.Medium" />
    </ScrollView>
    

    However if you don't plan to have more than one views scrollable you should remove the ScrollView and use:

       android:scrollbars="vertical"
    

    attribute on your TextView since the TextView is scrollable itself.