Ellipsize not working in this table layout

Go To StackoverFlow.com

0

I am currently trying to learn Android. Have been reading tutorials and manual for some days now. I am stuck at a layout issue.

I'm scrapping the content from a webpage and displaying it to the user. That's the working of my app.

My layout is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:padding="5px">

<TextView
    android:id="@+id/tvOutput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_marginTop="5dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0" android:id="@+id/tblOutput">

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

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

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

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

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

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

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

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

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

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

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

        </TableLayout>

        <Button
            android:id="@+id/btnBack"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/lblBack" android:layout_marginTop="10dip"/>


        <Button
            android:id="@+id/btnSendSMS"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="@string/lblSendSMS" />

    </LinearLayout>

</ScrollView>

Via code, I'll loop through the TableRows and add two TextViews each. Below is a sample piece of it:

                for(i=1;i<=11;i++){
                    TextView tv1 = new TextView(getApplicationContext());
                    TextView tv2 = new TextView(getApplicationContext());

                    //get values and store it in strValue1 & strValue2
                    //....

                    tv1.setText(strValue1); //a long text
                    tv2.setText(strValue2); //would always be a 3 letter value
                    //tv1.setEllipsize(TruncateAt.MARQUEE);
                    //tv1.setMarqueeRepeatLimit(1);
                    tv1.setSingleLine(true);
                    tv1.setEllipsize(TruncateAt.END);
                    tv1.setGravity(Gravity.LEFT);


                    tr = (TableRow) findViewById( getResources().getIdentifier("tableRow"+i, "id", getPackageName()));
                    tr.addView(tv1);
                    tr.addView(tv2);
                }

The problem is, setEllipsize() for the first TextView on each TableRow is not doing anything. It just shows the text of first TextView (long text overflowing the width) and the TextView2 is not even displayed.

When I tried the setEllipsize() and setSingleLine() on an existing TextView (the very first TextView in my layout - you could see from the above XML, which is above the Table Layout), it's working fine.

Any ideas where I went wrong ? Or any suggestions ?

Thanks in advance :)

2012-04-05 18:10
by Akhilesh B Chandran


0

You need to set a fixed height for the TextView with any one of the ways possible. Setting the number of lines or just setting the height of the TextView layout. If you dont then since ure textview has wrap_content as height, it will resize and show all the text.

So first fix the size of the textview and then setellipsize()

2012-04-05 18:19
by Shubhayu
Thanks :) I have tried setting the max lines of the TextView using setMaxLines(1). But that's not working. I will try setting the Layout_Height to fill_parent. For this I have to create a TableLayout Params and add the rules to it - Akhilesh B Chandran 2012-04-06 02:37
Play with the width too. SOmetimes in wrap content, where the width changes based upon the content, this issue is seen - Shubhayu 2012-04-06 04:33
I have tried setting fillparent to all those parents of TextViews. But that didn't worked. Now trying to play with Width. To set the layout params (FILLPARENT) for the TextView residing in a TableRow (inside a TableLayout), do I have to create an object of TableLayout and pass it to setLayoutParams of TextView - Akhilesh B Chandran 2012-04-08 06:13
I have also tried the idea of separating the layout for TableRow and then inflating via code, based on this solution: http://stackoverflow.com/questions/2684775/android-two-issues-using-tablerowtextview-in-tablelayout/2685130 In the layout for the row, I have added two TextViews and then set the LayoutWidth to FILL_PARENT, Gravity to LEFT, SingLines to TRUE for the first TextView. But it is showing the same way like before. : - Akhilesh B Chandran 2012-04-08 06:57
Can you fix the TextWidth to somehting like 100 dpi and then try it? I noticed u have a scrollview, which lets the components adjust size. And fill_parent doesnt help because it is in a scrollview. So fix your width and height to 100x72dpi (thats about 2 lines high - Shubhayu 2012-04-08 07:07
Thanks that works when I set the width to 100dp. But my ScrollView is a vertical scrollview. So that means it would adjust the size vertically. Isn't it - Akhilesh B Chandran 2012-04-08 07:14
ok try this (though i know u got it working), Based on what i have understood, set (all widths) scrollview to fillparent, the linearlayout inside it to fillparent, tablelayout to fillparent, table row to fillparent. Now for both the textviews, make their width fillparent and give them weight = 1. If fillparent doesn't work try wrap_content. (I couldn't try this yet - Shubhayu 2012-04-08 08:18
Thanks, I'll try that. When I have read the manual of TableRow, it says, children of a TableRow do not need to specify the layoutwidth and layoutheight attributes - Akhilesh B Chandran 2012-04-08 13:33
I have tried assigning Layout_Weight. Tried assigning both TVs 1. Then tried doing it like mentioned in here: http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean (with height = 0dp). Tried changing and resetting layout widths. None of them worked ! So, I put back the Width = 100dp and working fine. : - Akhilesh B Chandran 2012-04-08 14:06
Ads