Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to truncate TextView in TableLayout?

Tags:

android

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));
like image 702
ab11 Avatar asked Dec 08 '25 10:12

ab11


1 Answers

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>
like image 142
Nicklas A. Avatar answered Dec 10 '25 01:12

Nicklas A.