Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate TextView, and then add ellipsis in Android

Tags:

android

I have read a few of the other threads here with similar concerns, but none of their answers seemed to work for me.

Can't get ellipsis to work on Android

Android: Something better than android:ellipsize="end" to add "..." to truncated long Strings?

I have a Relative Layout with 2 elements, a text view and an image view. I need the text view to be 1 line and to truncate about 5sp short of the image view and apply ellipsis to the end of the text.

<RelativeLayout
                android:id="@+id/title_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5sp">

                <TextView
                    android:id="@+id/"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:textSize="10sp"
                    android:textColor="#FFFFFF"
                    android:text="title"
                    android:maxLines="1"
                    android:ellipsize="end"/>

                <ImageView
                    android:id="@+id/"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="5sp"
                    android:layout_marginLeft="5sp"
                    android:background="@drawable/home"/>

</RelativeLayout>

In the above code, I was attempting to apply a margin left to the Image View to force the text view to its left to truncate by as many sp as I put in the margin.

The code above would truncate the text view to a single line and add (...) but only to the end of that line; irrespective of the ImageView to it's right, regardless of whatever margin I apply to it. The Image View appears above the text. I figured this may be due to them being in a relative layout.

Any help in figuring out how to make the text view respect the margin/space taken up by the image view would be greatly appreciated.

like image 398
wapples Avatar asked Oct 08 '12 20:10

wapples


1 Answers

I believe what you want to do is to first place the image view (with margins) and then set the text to align to the left border and to be placed to the leftOf the image.

like image 180
Tobias Ritzau Avatar answered Sep 19 '22 10:09

Tobias Ritzau