jmendegan jmendegan - 24 days ago 10
Android Question

Can someone figure out what is wrong with this TableLayout?

I have this table and it displays some names along with some numbers for weight, cost, and quantity. When I tried to make another row under another heading ("ID Numbers") the rows get all out of whack. If I wrap_content it seems to fix it but then there is another problem, which is why I'm here. The ID number in the cell that has wrap_content WILL NOT fill the rest of the cell. If someone can explain what is wrong with this cell please tell me!

table.xml:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#222222"
android:shrinkColumns="*">
<TableRow android:layout_margin="1dp" android:background="#8b4513" android:gravity="center">
<TextView
android:padding="3dip"
android:text="Basic Info"
android:textColor="#000000"
android:textSize="22dp"
android:gravity="center" />

</TableRow>
<TableRow android:layout_margin="1dp" android:background="#f0e68c" android:gravity="left">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Name"
android:textColor="#000000"
android:textSize="22dp" />
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Weight"
android:textColor="#000000"
android:textSize="22dp" />

<TextView
android:gravity="right"
android:padding="3dip"
android:text="Cost"
android:textColor="#000000"
android:textSize="22dp" />

<TextView
android:gravity="left"
android:padding="3dip"
android:text="Qty"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#708090">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Shovel"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="center"
android:padding="3dip"
android:text="6"
android:textColor="#000000"
android:textSize="22dp" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="50"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="23"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#708090">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Shoes"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="center"
android:padding="3dip"
android:text="2"
android:textColor="#000000"
android:textSize="22dp" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="4"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="6"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#708090">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Boots"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="center"
android:padding="3dip"
android:text="2"
android:textColor="#000000"
android:textSize="22dp" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="5"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="5"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#708090">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Gloves"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="center"
android:padding="3dip"
android:text="1"
android:textColor="#000000"
android:textSize="22dp" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="10"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="6"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#708090">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Better gloves"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="center"
android:padding="3dip"
android:text="2"
android:textColor="#000000"
android:textSize="22dp" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="5"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="5"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#708090">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Hat"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="center"
android:padding="3dip"
android:text="1"
android:textColor="#000000"
android:textSize="22dp" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="23"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="right"
android:padding="3dip"
android:text="11"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#8b4513" android:gravity="center">
<TextView
android:padding="3dip"
android:text="ID Numbers"
android:textColor="#000000"
android:textSize="22dp"
android:gravity="center" />


Here is the rest of the code. The ID number does not fill the whole cell:

</TableRow>
<TableRow android:layout_margin="1dp" android:background="#708090">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Shovel ID"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
<TextView
android:gravity="left"
android:padding="3dip"
android:text="12736152889977"
android:width="wrap_content"
android:textColor="#000000"
android:textSize="22dp"
android:background="#aaaaaa" />
</TableRow>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>

Answer

Try this way

The Error is showing because in ScrollView you can only have one Child (or View) but that one child can contain many other, like this.

Take one LinearLayout in your ScrollView and in LinearLayout create your two TableLayout. Here LinearLayout will be only one child of ScrollView but LinearLayout will have two childs (Two TableLayouts).

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#222222"
            android:shrinkColumns="1" >

            <TableRow
                android:layout_margin="1dp"
                android:background="#8b4513"
                android:gravity="center" >

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="Basic Info"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#f0e68c"
                android:gravity="left" >

                <TextView
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Name"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Weight"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="Cost"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Qty"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#708090" >

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Shovel"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="6"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="50"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="23"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#708090" >

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Shoes"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="2"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="4"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="6"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#708090" >

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Boots"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="2"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="5"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="5"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#708090" >

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Gloves"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="1"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="10"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="6"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#708090" >

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Better gloves"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="2"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="5"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="5"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#708090" >

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Hat"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="1"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="23"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:gravity="right"
                    android:padding="3dip"
                    android:text="11"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>
        </TableLayout>

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#222222"
            android:shrinkColumns="1" >

            <TableRow
                android:layout_margin="1dp"
                android:background="#8b4513"
                android:gravity="center" >

                <TextView
                    android:gravity="center"
                    android:padding="3dip"
                    android:text="ID Numbers"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>

            <TableRow
                android:layout_margin="1dp"
                android:background="#708090" >

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="Shovel ID"
                    android:textColor="#000000"
                    android:textSize="22dp" />

                <TextView
                    android:background="#aaaaaa"
                    android:gravity="left"
                    android:padding="3dip"
                    android:text="12736152889977"
                    android:textColor="#000000"
                    android:textSize="22dp" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</ScrollView>

Comments