views:

3073

answers:

3

Hi,

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>

+4  A: 

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.

fiXedd
Thanks, but unfortunately it still doesn't work. Could it have something to do with the tableLayout?
aspartame
It WAS the `TableLayout`... updated answer above.
fiXedd
Excellent, thank you!
aspartame
NP... also, WRT the TableLayout definition: You only need to declare `xmlns:android` on the very first XML element.
fiXedd
This work-around is not working for me. I have a ListView, each item in the list has multiple text views in a TableLayout. When I added inputType='text' to the text view that can grow long, I can no longer select items from the list. Plus, I still did not see the ellipses. I did vote for the bug but it is closed and not reproducible. Perhaps a new bug report can be created. Any other ideas?
John in MD
A: 

I found somewhere this attributes

<TextView ...
android:singleLine="true"
android:ellipsize="marquee" />

it work fine for me (Android 1.5)

molokoloco
singleLine is deprecated in 1.5
fiXedd
+2  A: 

try...

i.e. android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="num|marquee_forever" android:lines="1" android:focusableInTouchMode="true" android:scrollHorizontally="true"

as marquee only works when view is selected or focused according to the comment#2 on http://code.google.com/p/android/issues/detail?id=5364

hwii77
I personally found that `android:ellipsize="marquee" android:scrollHorizontally="true" android:lines="1" ` was all you needed.
Steve Pomeroy