giozh
giozh

Reputation: 10068

Two tableLayout inside a linearLayout

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?

Upvotes: 1

Views: 3094

Answers (3)

Kirk
Kirk

Reputation: 16245

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?

Upvotes: 4

JRowan
JRowan

Reputation: 7114

put the LinearLayout inside a:

<ScrollView/>    </ScrollView>

Upvotes: 1

Thibault D.
Thibault D.

Reputation: 10004

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.

Upvotes: 2

Related Questions