Search code examples
androidandroid-layoutandroid-scrollviewandroid-layout-weight

Setting ScrollView weight makes View not shown


I'm trying to set 50% of height to a ScrollView. For this, I'm using android:layout_height="0dp" and android:layout_weight="0.5" but the ScrollView and the LinearLayout after it are not being displayed. Here is my .xml

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

    <TextView       
        android:id="@+id/registerProductTitle"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="20dip"
        android:layout_marginBottom="20dip"
        android:gravity="center"
        android:text="@string/registerProductTitle"
        android:textSize="12pt">
    </TextView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp" // 50% height
        android:layout_weight="0.5" // 50% height
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip">

        <TableLayout
            android:id="@+id/mainTable"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:paddingLeft="3dip"
            android:paddingRight="3dip"
            android:shrinkColumns="1"
            android:stretchColumns="*">

            <TableRow>
                <TextView
                    android:id="@+id/productLabel"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/productLabel"
                    android:textSize="10pt">
                </TextView>

                <EditText
                    android:id="@+id/productName"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:hint="@string/productTextHolder"
                    android:inputType="textPersonName" >
                </EditText>
            </TableRow>         
        </TableLayout> 
    </ScrollView>       

    <LinearLayout // This Layout is not being displayed
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/buttonSaveProduct"
            style="?android:attr/buttonStyleSmall"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:layout_gravity="bottom"
            android:text="@string/saveProduct" />
    </LinearLayout>
</LinearLayout>

Solution

  • If you are using the weight attribute for the children of a LinearLayout, make sure to define it for every child, or you can have interesting output.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout    
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    
        <TextView       
            android:id="@+id/registerProductTitle"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="20dip"
            android:layout_marginBottom="20dip"
            android:gravity="center"
            android:text="@string/registerProductTitle"
            android:layout_weight="0.25"
            android:textSize="12pt">
        </TextView>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip">
    
            <TableLayout
                android:id="@+id/mainTable"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:paddingLeft="3dip"
                android:paddingRight="3dip"
                android:shrinkColumns="1"
                android:stretchColumns="*">
    
                <TableRow>
                    <TextView
                        android:id="@+id/productLabel"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:text="@string/productLabel"
                        android:textSize="10pt">
                    </TextView>
    
                    <EditText
                        android:id="@+id/productName"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:hint="@string/productTextHolder"
                        android:inputType="textPersonName" >
                    </EditText>
                </TableRow>         
            </TableLayout> 
        </ScrollView>       
    
        <LinearLayout
            android:layout_weight="0.5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1"
            android:gravity="bottom"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/buttonSaveProduct"
                style="?android:attr/buttonStyleSmall"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_width="0dip"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:layout_gravity="bottom"
                android:text="@string/saveProduct" />
        </LinearLayout>
    </LinearLayout>
    

    I added weight for the LinearLayout below, and for the TextView above.

    See more information here: Android Developers | LinerLayout