Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ellipsize not working for textView inside custom listView

I have a listView with custom objects defined by the xml-layout below. I want the textView with id "info" to be ellipsized on a single line, and I've tried using the attributes

android:singleLine="true" android:ellipsize="end" 

without success.

If I set the layout_width to a fixed width like e.g.

android:layout_width="100px" 

the text is truncated fine. But for portability reasons this is not an acceptable solution.

Can you spot the problem?

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="5px" > <TextView   android:id="@+id/destination" android:layout_width="fill_parent"  android:layout_height="wrap_content" android:textSize="22dp" android:paddingLeft="5px" />  <TextView   android:id="@+id/date" android:layout_width="fill_parent"  android:layout_height="wrap_content" android:textSize="15dp" android:paddingLeft="5px" />  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/info_table" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="5px" android:paddingTop="10px"  >     <TableRow>         <TextView             android:id="@+id/driver_label"             android:gravity="right"             android:paddingRight="5px"             android:text="@string/driver_label" />         <TextView             android:id="@+id/driver" />     </TableRow>     <TableRow>         <TextView             android:id="@+id/passenger_label"             android:gravity="right"             android:paddingRight="5px"             android:text="@string/passenger_label" />         <TextView             android:id="@+id/passengers" />     </TableRow>     <TableRow>         <TextView             android:id="@+id/info_label"             android:gravity="right"             android:paddingRight="5px"              android:text="@string/info_label"/>         <TextView             android:id="@+id/info"             android:layout_width="fill_parent"             android:singleLine="true"             android:ellipsize="end" />     </TableRow> </TableLayout>  

like image 725
aspartame Avatar asked Sep 14 '09 22:09

aspartame


People also ask

Can we Nest TextView inside TextView?

In Android all layout can be nested one another.

What is Ellipsize in TextView?

Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.

What is Ellipsize marquee?

If you want a horizontal scrollable text in your app, use android:ellipsize="marquee" where a single line large text will be scrolling.


2 Answers

Without using deprecated properties, the following lines work great for me

android:ellipsize="end"  android:lines="1" android:scrollHorizontally="true" 

ref http://code.google.com/p/android/issues/detail?id=882#c9

like image 117
tbruyelle Avatar answered Sep 28 '22 05:09

tbruyelle


Ellipsize is broken (go vote on the bug report, especially since they claim it's not reproducible) so you have to use a minor hack. Use:

android:inputType="text" android:maxLines="1" 

on anything you want to ellipsize. Also, don't use singleLine, it's been deprecated.

UPDATE:

On closer inspection, the problem you're having is that your table is extending off the right side of the screen. Changing your TableLayout definition to:

<TableLayout     android:id="@+id/info_table"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:paddingLeft="5px"     android:paddingTop="10px"     android:shrinkColumns="1"> 

should fix that problem, then do what I said above to ellipsize your TextView.

like image 37
Jeremy Logan Avatar answered Sep 28 '22 05:09

Jeremy Logan