1

I would place two table layout inside a Linearlayout. But with my code, I can see only one table. That's the code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:stretchColumns="1" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+text_view/tw_nome"
            android:text="@string/tw_nome" />

        <EditText
            android:id="@+edit_text/et_nome"
            android:hint="@string/et_nome"
            android:imeOptions="actionNext"
            android:singleLine="true" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+text_view/tw_cognome"
            android:text="@string/tw_cognome" />

        <EditText
            android:id="@+edit_text/et_cognome"
            android:hint="@string/et_cognome"
            android:imeOptions="actionDone"
            android:singleLine="true" />
    </TableRow>
</TableLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+text_view/tw_sesso"
            android:text="@string/tw_sesso" />

        <RadioGroup
            android:id="@+radio_group/rg"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+radio_button/button_m"
                android:text="@string/button_m" />

            <RadioButton
                android:id="@+radio_button/button_f"
                android:text="@string/button_f" />
        </RadioGroup>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+button/button_salva"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_salva" />

        <Button
            android:id="@+button/button_annulla"
            android:text="@string/button_annulla" />
    </TableRow>
</TableLayout>

what's wrong?

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
giozh
  • 9,868
  • 30
  • 102
  • 183

3 Answers3

4

Side-by-side or vertical?

If you're looking for side-by-side, try layout_weight. Also note the change in layout_width.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <TableLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:stretchColumns="1" >
            <!-- rows -->
    </TableLayout>

    <TableLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:stretchColumns="1" >
            <!-- rows -->
    </TableLayout>
</LinearLayout>

Update From the comments it was asked why layout_width should be set to 0dp.

Setting 0dp is an optimization when you use layout_weight. Setting width to 0dp tells the layout to ignore layout_height and use layout_weight instead which in this case gives an even width to each TableLayout.

I actually asked this same question and got some useful answers if it's helpful Why is 0dp considered a performance enhancement?

Community
  • 1
  • 1
Kirk
  • 16,182
  • 20
  • 80
  • 112
  • *This is if your LinearLayout's orientation is horizontal. Switch the width/height directives if your orientation is vertical. – Thibault D. Feb 18 '13 at 21:57
  • can i ask you why the "0dp" for width or height? – giozh Feb 19 '13 at 09:17
  • I know it sounds awkward, but setting `0dp` is an optimization when you use `layout_weight`. Setting width to `0dp` tells the layout to ignore `layout_height` and use `layout_weight` instead. – Kirk Feb 19 '13 at 14:20
  • I asked the same question and posted as question on this and got some useful answers. Take a look at http://stackoverflow.com/questions/12016781/why-is-0dp-considered-a-performance-enhancement – Kirk Feb 19 '13 at 14:22
2

Both table layouts widths and heights are defined as match_parent, so they are taking the same space as the parent. You don't have enough space in the parent for tow children to display both with the same size as the parent.

How you would solve this depends on the parent LinearLayout's orientation.

Matt Busche
  • 14,216
  • 5
  • 36
  • 61
Thibault D.
  • 10,041
  • 3
  • 25
  • 56
1

put the LinearLayout inside a:

<ScrollView/>    </ScrollView>
JRowan
  • 6,824
  • 8
  • 40
  • 59