Android table layout formatting problems

I can not get my TableLayout to fit inside the screen when the text cell is large, despite the text wraps inside the cell. I have been googling for an answer but it I can not find an advice that works for me. 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. Thanks in advance Sammy

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);

    }
} 

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


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.