0

I have a problem similar to this question: Android Layout Weight but it do not seems to have the same answer...

Inside a tablerow, I would like to display TextViews and EditTexts, where the TextViews take minimal size, and EditTexts share the remaining size:

    'Size:[   edit1   ] x [   edit2   ] pixels'

I would guess the following implementation, but it displays:

    'Size:                      [] x [] pixels'

What is wrong ?

    <TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="1"
    android:gravity="center" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Size:" />

    <EditText
        android:id="@+id/desired_width"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="number" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="x" />

    <EditText
        android:id="@+id/desired_height"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="number" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="pixels" />
</TableRow>

I'm under 100 reputation and cannot answer my own question, but zapl is right, it's a problem oh behavior between TableRow and LinearLayout:

The solution if to create a LinearLayout inside the TableRow:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="5dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Size:" />

        <EditText
            android:id="@+id/desired_width"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="number" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x" />

        <EditText
            android:id="@+id/desired_height"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="number" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="pixels" />
    </LinearLayout>
</TableRow>
Community
  • 1
  • 1
cyrilchampier
  • 2,207
  • 1
  • 25
  • 39

1 Answers1

0

android:layout_weight works for LinearLayout only.

Something like this might work for you:

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1,3" >

that should grow columns 1 and 3 (starting at 0)

zapl
  • 63,179
  • 10
  • 123
  • 154
  • It's indeed a problem on my example ! But it do not solve the proble inside the table row :( – cyrilchampier Mar 18 '12 at 15:21
  • But there was the same kind of problem on the TableRow: it do not behave like a LinearLayout, I had to nest a LinearLayout in it to have the correct layout. – cyrilchampier Mar 18 '12 at 15:31