Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android table layout formatting problems

Tags:

I can not get my TableLayout to fit inside the screen when the text cell is large, despite the text wraps inside the cell.

This is my TableLayout. A simple two-rows, two-columns table. Left column cells are multiline. If the text of one of these cell is large enought to break in two lines, the column is stretched to fill the entire screen and the right columns is pushed out of the screen.

Why? How can I force the full table to stay inside the screen? Any hint is welcomed.

My TableLayout is the following:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myTableLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow>     <TextView         android:text="Account 4 with a very large name to see if it breaks  on two or more lines"         android:singleLine="false"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         >     </TextView>     <TextView         android:text="$400.00"         android:gravity="right"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         >     </TextView> </TableRow>  <TableRow>     <TextView         android:text="Account 5"         android:singleLine="false"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         >     </TextView>     <TextView         android:text="$400.00"         android:gravity="right"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         >     </TextView> </TableRow>  </TableLayout> 

The code to display the layout is basic:

public class AccountBrowserTest      extends Activity {     public void onCreate(Bundle savedInstanceState)      {             super.onCreate(savedInstanceState);          this.setContentView(R.layout.accountbrowser_test);      } }  
like image 960
sammybar Avatar asked Dec 01 '10 21:12

sammybar


People also ask

Is table layout deprecated?

This constant is a layoutMode . This constant was deprecated in API level 28. The view drawing cache was largely made obsolete with the introduction of hardware-accelerated rendering in API 11.

Is table layout available in Android?

Android TableLayout going to be arranged groups of views into rows and columns. You will use the <TableRow> element to build a row in the table. Each row has zero or more cells; each cell can hold one View object. TableLayout containers do not display border lines for their rows, columns, or cells.

What is a table layout in Android?

TableLayout is a ViewGroup that displays child View elements in rows and columns. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout. TableLayout positions its children into rows and columns.

What is the use of table layout?

Android TableLayout is a ViewGroup subclass which is used to display the child View elements in rows and columns. It will arrange all the children elements into rows and columns and does not display any border lines in between rows, columns or cells.


2 Answers

Add android:stretchColumns and android:shrinkColumns to the TableLayout element:

<TableLayout   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:stretchColumns="1"   android:shrinkColumns="1"> 

The 1 values are the columns you want the changes to occur. You can specify more that one value like this:

android:stretchColumns="0,1" 

or if you want the changes to happen to all you columns like this:

android:stretchColumns="*" 

Please look here for detailed information: http://android-pro.blogspot.com/2010/02/table-layout.html

like image 109
Paul Avatar answered Sep 28 '22 02:09

Paul


i had the same problem with my app and found the following solution:

<TableLayout     android:shrinkColumns="0"    android:stretchColumns="1"     android:layout_width="match_parent"    android:layout_height="wrap_content"> </TableLayout> 

This works fine with 2 Columns.

like image 27
Jonathan Roth Avatar answered Sep 28 '22 03:09

Jonathan Roth