2

I have a TextView in a column of my TableLayout. I would like the TextView to truncate and ellipsize if its text is longer than the column can accommodate, but it just stretches the column and destroys my layout. None of the suggestions for TextView truncating I have found have worked, I'm assuming this is due to the Table. Any suggestions?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </TextView>
    <RelativeLayout  
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ImageView 
            android:id="@+id/Icon"
            android:src="@drawable/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </ImageView>
        <TextView 
            android:id="@+id/Value"
            android:layout_toRightOf="@id/Icon"
            android:text="very long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long string"
            android:inputType="text"
            android:maxLines="1"
            android:ellipsize="end"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
    </RelativeLayout>

*EDIT: Code for the table:

int fiveDip = convToDip(5, getContext());

Table table = new TableLayout(getContext());
table.setPadding(fiveDip, fiveDip, fiveDip, fiveDip);
table .setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
ab11
  • 19,770
  • 42
  • 120
  • 207

2 Answers2

13

You need to set both shrinkColumns and stretchColumns on the column containing the TextView:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:shrinkColumns="1"
    android:stretchColumns="1">     
        <TableRow>
            <View 
                android:background="#f00"
                android:layout_height="fill_parent"
                android:layout_width="40dp" />

            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textSize="18sp"
                android:textColor="#fff"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Example text that is very loooooooooooooooooooooooooooooooooooooooooooooong" />

            <View 
                android:background="#00f"
                android:layout_height="fill_parent"
                android:layout_width="40dp" />
        </TableRow>

</TableLayout>
Nicklas A.
  • 6,501
  • 7
  • 40
  • 65
0

Your xml and code seems unrelated to the problem you've stated. Nevertheless, have you tried doing android:singleLine="true" for your TextView. For Marquee, see Android automatic horizontally scrolling TextView

Also, you can wrap your TableLayout with a ScrollView to enable scrolling.

Community
  • 1
  • 1
Rajath
  • 11,787
  • 7
  • 48
  • 62
  • I tried with just adding singleLine="true", and then by using your code for marquee. Neither worked. A would prefer to truncate, than to wrap with a scrollview. – ab11 Apr 20 '11 at 20:17