Question

I have a TableLayout to show some data. Each row has two columns: the first one has the field name and the second one has the field value. If any of the fields is clicked I show an EditText. I do it using a ViewSwitcher.

When the TextView/EditText have just a few characters the view is fine, but if the first line is full the first column (field names) is becomes more narrow and the second column becomes wider. I don't want that to happen, I want my columns to be always the same size.

This is my layout:

<TableLayout
    android:id="@+id/formLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textLicense"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/license"
            android:textSize="18sp"
            android:textStyle="bold" />

        <ViewSwitcher
            android:id="@+id/licenseSwitcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3" >

            <TextView
                android:id="@+id/tvLicenseVariable"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="LicenseViewClicked"
                android:textSize="18sp" />

            <com.timeondriver.petrolcontrol.MySpinner
                android:id="@+id/spinnerLicense"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:prompt="@string/license_prompt" />
        </ViewSwitcher>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/date"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvDateVariable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:clickable="true"
            android:onClick="DateViewClicked"
            android:textSize="18sp" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/textPlace"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/place"
            android:textSize="18sp"
            android:textStyle="bold" />

        <ViewSwitcher
            android:id="@+id/placeSwitcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3" >

            <TextView
                android:id="@+id/tvPlaceVariable"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="PlaceViewClicked"
                android:textSize="18sp" />

            <EditText
                    android:id="@+id/editTextPlace"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                android:imeOptions="actionDone"
            android:maxLines="3"
                    android:inputType="textMultiLine"
                     />
        </ViewSwitcher>
    </TableRow>
</TableLayout>

This is how the view looks like when the place data is short:

enter image description here

And this is how the view looks like when the place data has too many characters (first line full):

enter image description here

License, date and time rows aren't a problem since their size is limited. The row with the place data is the one that changes the column width when it has too many characters. How can I have always the same width in my columns?

Thanks!

Was it helpful?

Solution

I guess the problem is that you specified a layout_width and also a layout_weight. If you do not type in enough text the specified layout_width seems to be used wich is wrap_content. If you type in enough text you reach a width which would be greater than the specified layout_weight so at this point the layout_weight attribute counts. To always have the same size of colums try to set the layout_width to 0dpi so only the layout_weight attribute will be used. Then you have to specify a different ratio bewteen the two columns because it seems 1/4 to 3/4 does not seem to fit for the texts in the left column. Maybe you should choose something like 2/5 to 3/5 which means layout_weight=2 for the left column and 3 for the right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top