Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Ellipsize on TextView reduces lines shown by one (instead of only ellipsizing last)

when I am using TextView with singleLine="true" and ellipsize="end"(my top TextView), it works well

but in another TextView having more then 1 lines (in my case 3 lines in my bottom TextView), lines="3" and maxLines="3" and ellipsize="end", doesn't work properly.

When I DON'T put ellipsize="end" in tvDesc, it shows 3 line, which is OK. Here is code : XML :

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <ImageView         android:id="@+id/imgv"         android:layout_width="65dp"         android:layout_height="80dp"         android:layout_alignParentLeft="true"         android:layout_alignParentTop="true"         android:src="@drawable/img1"         android:scaleType="fitXY" />      <TextView         android:id="@+id/tvTitle"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentTop="true"         android:layout_toRightOf="@+id/imgv"         android:background="@android:color/white"         android:textColor="@android:color/black"         android:text="Title testing line number and ellipsize at end"         android:maxLines="1"         android:singleLine="true"         android:ellipsize="end"    <---  WORKS WELL         android:textAppearance="?android:attr/textAppearanceMedium" />      <TextView         android:id="@+id/tvDesc"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/tvTitle"         android:layout_toRightOf="@+id/imgv"         android:layout_alignBottom="@+id/imgv"         android:layout_alignParentRight="true"         android:textSize="14dp"         android:lines="3"         android:maxLines="3"                        <----------  WITHOUT ellipsize                  android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted"         android:textAppearance="?android:attr/textAppearanceMedium" />  </RelativeLayout> 

Above code shows TextView tvDesc with 3 Lines and No-Ellipsize. Here is Image :

enter image description here

But, I wanna ellipsize, so I uses followin code : XML :

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <ImageView         android:id="@+id/imgv"         android:layout_width="65dp"         android:layout_height="80dp"         android:layout_alignParentLeft="true"         android:layout_alignParentTop="true"         android:src="@drawable/img1"         android:scaleType="fitXY" />      <TextView         android:id="@+id/tvTitle"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentTop="true"         android:layout_toRightOf="@+id/imgv"         android:background="@android:color/white"         android:textColor="@android:color/black"         android:text="Title testing line number and ellipsize at end"         android:maxLines="1"         android:singleLine="true"         android:ellipsize="end"         android:textAppearance="?android:attr/textAppearanceMedium" />      <TextView         android:id="@+id/tvDesc"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/tvTitle"         android:layout_toRightOf="@+id/imgv"         android:layout_alignBottom="@+id/imgv"         android:layout_alignParentRight="true"         android:textSize="14dp"         android:lines="3"         android:maxLines="3"         android:ellipsize="end"    <------  WITH  ELLIPSIZE                  android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted"         android:textAppearance="?android:attr/textAppearanceMedium" />  </RelativeLayout> 

Which shows TextView tvDesc with only 2 lines and Ellipsize,

which is not properly dezired UI, as Followin: enter image description here

I wanna 3 lines in TextView tvDesc with ellipsize

CAN ANYONE HELP ME.?

like image 811
Chintan Raghwani Avatar asked Jun 26 '12 15:06

Chintan Raghwani


People also ask

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

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.


1 Answers

This is by far the simplest solution I've found and am currently using in deployment. Let me know if you need any other assistance!

Oh and remember to remove the android:ellipsize tag in your XML since you will be using the bottom code to automatically ellipsize at the end of 3 lines.

TextView snippet; snippet.setText("loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor ") ViewTreeObserver vto = this.snippet.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {      @Override     public void onGlobalLayout() {         ViewTreeObserver obs = snippet.getViewTreeObserver();         obs.removeGlobalOnLayoutListener(this);         if (snippet.getLineCount() > 3) {             int lineEndIndex = snippet.getLayout().getLineEnd(2);             String text = snippet.getText().subSequence(0, lineEndIndex - 3) + "...";             snippet.setText(text);         }     } }); 
like image 179
Vishwa Patel Avatar answered Sep 19 '22 07:09

Vishwa Patel