android: two issues using Tablerow+TextView in Tablelayout
Asked Answered
L

3

8

I am using Tablerow+TextView to make a simple view for blog posts and their replies. In each TableRow I put a TextView in. Now I have two issues:

  1. The text which is longer than the screen won't automatically wrap up to be multi-line. Is it by design of TableRow? I've already set tr_content.setSingleLine(false); [update] This has been addressed, I think I should change Fill_parent to be Wrap_content in textView.tr_author_time.setLayoutParams(new LayoutParams( LayoutParams.**WRAP_CONTENT**, LayoutParams.WRAP_CONTENT));

  2. The Table won't scroll like ListView. My rows are more than the screen size. I expect the table could be scrolled down for viewing just like ListView. Is that possible?

Here is my code:

    TableLayout tl = (TableLayout) findViewById(R.id.article_content_table);
        TextView tr_title = new TextView(this);
    TextView tr_author_time = new TextView(this);
    TextView tr_content = new TextView(this);
    TableRow tr = new TableRow(this);

    for(int i = 0; i < BlogPost.size(); i++){
        try{
        // add the author, time
        tr = new TableRow(this);
        /////////////////add author+time row
        BlogPost article = mBlogPost.get(i);
        tr_author_time = new TextView(this);
        tr_author_time.setText(article.author+"("+
                article.post_time+")");
        tr_author_time.setTextColor(getResources().getColor(R.color.black));
        tr_author_time.setGravity(0x03);
        tr_author_time.setLayoutParams(new LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT)); 
        tr.addView(tr_author_time); 
        tl.addView(tr,new TableLayout.LayoutParams( 
                LayoutParams.FILL_PARENT, 
                LayoutParams.WRAP_CONTENT));
        ////////////////////// then add content row
        tr = new TableRow(this);            
        tr_content = new TextView(this);
        tr_content.setText(article.content);
        tr_content.setSingleLine(false);
        tr_content.setGravity(0x03);
        tr_content.setLayoutParams(new LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT));            
        tr.addView(tr_content);       
         tr.setBackgroundResource(R.color.white);
            tl.addView(tr,new TableLayout.LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT));

    }   
Lupus answered 21/4, 2010 at 16:34 Comment(1)
How can we make two rows of equal Height ? Please see my question https://mcmap.net/q/247010/-how-to-customize-the-layout-height-and-width-and-containership-of-layouts/720176Bussard
M
14

This isn't really a complete answer, but it really seems like you're doing this the hard way.

Instead of constructing your TableRows manually, you should set them up in xml like this:

tablerow.xml:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/content"
        android:singleLine="false"
        android:textAppearance="@style/someappearance" />
</TableRow>

Prior to your loop, get a reference to a LayoutInflater:

LayoutInflater inflater = getLayoutInflater();

Then, inside your loop, create an instance of tablerow using the LayoutInflater:

TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
TextView content = (TextView)row.findViewById(R.id.content);
content.setText("this is the content");

tl.addView(row);

This will allow you to set your layout, appearance, layout params in xml making it much easier to read and debug.

For the scrolling problem, you'll need to add your TableLayout to a ScrollView. Something like this in your xml:

<ScrollView>
    <TableLayout android:id="@+id/arcitle_content_table" />
</ScrollView>
Masoretic answered 21/4, 2010 at 17:26 Comment(8)
It seems that adding <TableLayout> into <scrollView> is not possible. The program exits with error. In fact, I see only Views can be added into ScrollView but not Layout.Lupus
A TableLayout is a descendant of View. It should work just fine. What is the error?Masoretic
Works. Forgot to add width and height attributes to ScrollView. Thanks synic!Lupus
@Masoretic can you please edit tablerow.xml file with full code please. I am getting some error, and don't know what should I add more in that layout.Bekha
something is wrong! TableRow row = (TableRow)inflater.inflate(R.id.tablerow, tl, false); TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false); IT IS OKThee
How can we make two rows of equal Height ? Please see my question https://mcmap.net/q/247010/-how-to-customize-the-layout-height-and-width-and-containership-of-layouts/720176Bussard
@guest I am getting the same error. TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false); is NOT right! I tried: TableRow row = (TableRow)inflater.inflate(R.id.my_row_id, tl, false); and got this error: FATAL EXCEPTION: main- java.lang.RuntimeException: android.content.res.Resources$NotFoundException: Resource ID #0x7f070012 type #0x12 is not valid How can I solve this problem?Rolland
@synic, i am having 5 columns into table_row.xml, now i want only single i.e. 5th TextView in multi-line. is it possible or any other way to do so?Mueller
K
17

A more appropriate thing to do for wrapping items would have been to add android:shrinkColumns="*" or android:shrinkColumns="1" to the TableLayout, this would probably have fixed the wrapping issue.

For Details

Karilla answered 12/9, 2011 at 13:30 Comment(2)
+1 for sure. I was having a similar issue and this was the easiest / more robust fix. Thanks!Barsky
How can we make two rows of equal Height ? Please see my question https://mcmap.net/q/247010/-how-to-customize-the-layout-height-and-width-and-containership-of-layouts/720176Bussard
M
14

This isn't really a complete answer, but it really seems like you're doing this the hard way.

Instead of constructing your TableRows manually, you should set them up in xml like this:

tablerow.xml:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/content"
        android:singleLine="false"
        android:textAppearance="@style/someappearance" />
</TableRow>

Prior to your loop, get a reference to a LayoutInflater:

LayoutInflater inflater = getLayoutInflater();

Then, inside your loop, create an instance of tablerow using the LayoutInflater:

TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
TextView content = (TextView)row.findViewById(R.id.content);
content.setText("this is the content");

tl.addView(row);

This will allow you to set your layout, appearance, layout params in xml making it much easier to read and debug.

For the scrolling problem, you'll need to add your TableLayout to a ScrollView. Something like this in your xml:

<ScrollView>
    <TableLayout android:id="@+id/arcitle_content_table" />
</ScrollView>
Masoretic answered 21/4, 2010 at 17:26 Comment(8)
It seems that adding <TableLayout> into <scrollView> is not possible. The program exits with error. In fact, I see only Views can be added into ScrollView but not Layout.Lupus
A TableLayout is a descendant of View. It should work just fine. What is the error?Masoretic
Works. Forgot to add width and height attributes to ScrollView. Thanks synic!Lupus
@Masoretic can you please edit tablerow.xml file with full code please. I am getting some error, and don't know what should I add more in that layout.Bekha
something is wrong! TableRow row = (TableRow)inflater.inflate(R.id.tablerow, tl, false); TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false); IT IS OKThee
How can we make two rows of equal Height ? Please see my question https://mcmap.net/q/247010/-how-to-customize-the-layout-height-and-width-and-containership-of-layouts/720176Bussard
@guest I am getting the same error. TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false); is NOT right! I tried: TableRow row = (TableRow)inflater.inflate(R.id.my_row_id, tl, false); and got this error: FATAL EXCEPTION: main- java.lang.RuntimeException: android.content.res.Resources$NotFoundException: Resource ID #0x7f070012 type #0x12 is not valid How can I solve this problem?Rolland
@synic, i am having 5 columns into table_row.xml, now i want only single i.e. 5th TextView in multi-line. is it possible or any other way to do so?Mueller
P
7

To wrap text in table rows:

By default, TableLayout rows fit the width of their content, no matter it goes over the screen bounds. To get the wider-than-screen text cells to wrap to multi-line, use android:shrinkColumns attribute on TableLayout.

<TableLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*" />

android:shrinkColumns is zero-based index of the columns to shrink. It removes unnecessary extra space from a column and shrinks it :

  • android:shrinkColumns="*" shrinks all columns
  • android:shrinkColumns="0" shrinks first column
  • android:shrinkColumns="1,2" shrinks the second and third columns

android:stretchColumns does the opposite. It stretches a column to the maximum available width.

Both "shrink" and "stretch" consider all rows of the table to compute space.

To scroll down a TableLayout:

If your TableLayout is higher than the screen, move it in a ScrollView.

Podiatry answered 7/1, 2014 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.