Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TableLayout Width

I have a two column TableLayout as the only child of a scroll view. The first column contains TextViews ('labels') and the second column contains EditText/Spinner/DateWidget etc ('values'). Even though I have have specified android:layout_width="fill_parent" for TableLayout, TableRow & all widgets (in 'values' column).

The screen looks perfect when the activity is created. However, when one types a really long value in the EditText, the 'values' column goes beyond the visible screen area.

How do I tackle this?

like image 479
Sameer Segal Avatar asked Oct 30 '10 13:10

Sameer Segal


3 Answers

You may want to define the sizes of the columns by using a weight. So you will define the table layout height to fill parent but for each column you should set the width to "0px" and the weight to the percentage you want the column to span. So assuming you want the first column to be 30% of the screen width you set it's weight to "0.3" and the second column to "0.7".

Try it and see if that works.

like image 63
Savvas Dalkitsis Avatar answered Nov 05 '22 05:11

Savvas Dalkitsis


The pragmatic way to solve this is to use the stretchColumns and shrinkColumns attributes in TableLayout. Their values should be 0-based indexes of columns.

For example, if you have a two column Table layout and:

  • You would like the second column to fill up with empty space (stretch) so the TableLayout fits the entire parent view on a large screen device
  • You would like the first column to be shrunk (and its contents possibly wrapped / ellipsized) on a small screen device

you would define your TableLayout as:

<TableLayout
    android:id="@+id/my_table_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:shrinkColumns="0" >
</TableLayout>
like image 31
Jeshurun Avatar answered Nov 05 '22 06:11

Jeshurun


This worked for me..

tableLayout.setColumnShrinkable(1,true);
like image 7
Inco Mob Avatar answered Nov 05 '22 05:11

Inco Mob