10

I am using android:ellipsize="end" in android xml file, & surprisingly I am not getting the layout that I want, the 3 dots(...) are showing but after that dots there is another word truncated. Also this is a "not-always" behavior, check the ListView attached, sometimes, the behavior is normal & sometimes not.

Here's the screenshot of the layout from my device,

enter image description here

I don't have any idea why this is happening. Here's my xml file, having problem with the tv_news_content TextView -

<?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="100dp"
android:background="@color/white" >

<ImageView
    android:id="@+id/iv_next_tier"
    android:layout_width="18dp"
    android:layout_height="21dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:src="@drawable/right_arrow" >
</ImageView>

<TextView
    android:id="@+id/tv_news_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_toLeftOf="@+id/iv_next_tier"
    android:ellipsize="end"
    android:maxLines="2"
    android:text="News Title"
    android:textColor="@color/black"
    android:textSize="17dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tv_news_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tv_news_title"
    android:layout_below="@+id/tv_news_title"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/iv_next_tier"
    android:ellipsize="end"
    android:maxLines="2"
    android:text="News Contents"
    android:textColor="@color/black_light"
    android:textSize="15dp" />

<View
    android:id="@+id/view"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_below="@+id/tv_news_content"
    android:layout_marginTop="5dp" />

To make things clear, tv_news_title is the topmost bold TextView, & iv_next_tier is the small arrow-type ImageView at the right. & tv_news_content is the TextView that I am facing problem with.

Any solution why I am not getting desired output? Desired output means the always normal behaviour - I want those 3 dots at the end of second line of tv_news_content TextView, not before a truncated word.

Any suggestion is appreciated.

Khobaib
  • 1,577
  • 3
  • 21
  • 29
  • What is iv_next_tier ? I think it will help us better to see the entire xml or at least the RelativeLayout content.. – Cata Jun 16 '13 at 08:10
  • 1
    What happens if you change `maxLines="2"` to `lines="2"`? – Ken Wolf Jun 16 '13 at 08:16
  • @Cata, I mentioned already iv_next_tier is the small arrow-type ImageView, that you see at the right corner of the screenshot. I am updating the xml, will post full xml in a minute. – Khobaib Jun 16 '13 at 08:36
  • @KenWolf, I get same result :-( – Khobaib Jun 16 '13 at 08:40
  • @Khobaib Same problem. Did you resolve this issue? – dilettante Feb 10 '14 at 19:20
  • Hi @dilettante sorry for late reply, I somehow missed it. I resolved this issue somehow (I didn't find any proper solution, so did some hack). I replied in RuAware's post about my findings. Please take a look. – Khobaib May 28 '14 at 08:43

5 Answers5

14

Finally found what was causing this issue in my app!!

While RuAware's answer led me on the right path, I found that it didn't fix the problem for all of my text blocks. Given that it worked for some, I felt it was clearly due to some malicious character. So I went through one broken text block and just started removing characters until it stopped breaking.

It turns out it was the new line character '\n' that was causing it. Even if the new line was after the ellipsize, the new line character was [inconsistently] causing this problem as long as it was somewhere within the string.

The issue is solved by doing something similar to the following before setting the text to your view:

text = text.replace('\n',' ');
tabjsina
  • 1,582
  • 15
  • 26
  • 1
    Cool, let me know how it goes. The problem you responded with in RuAware's answer is probably because of the varying position of new lines in your strings – tabjsina Oct 01 '13 at 19:22
2

When setting the text for the textview make sure the text is not greater than 750 characters so use txt = theText.substring(0, 750) or something like that before calling settext. This works on the emulator with your feed. and should be enough characters for a 10inch tablet too

RuAware
  • 979
  • 1
  • 9
  • 26
  • I tried with trial & error, 750 doesn't work but when I make the text of 300 length at most, then it works for both item #3 & #4. When I used 500, it worked for #3 but not #4. I am wondering what's the reason behind it. Can you give me some light? – Khobaib Jun 16 '13 at 12:52
  • did u find any proper solution as this solution is not proper @Khobaib – Chirag Shah May 27 '14 at 10:18
  • no I didn't find any proper solution yet @ChiragShah – Khobaib May 28 '14 at 08:39
  • I've found that 500 is the lucky number with my situation. – nverbeek Dec 13 '16 at 17:14
0

Take a look at this post. There is a custom EllipsizingTextView which solves ellipsizing problem for multiline view. However it may solve you problem too.

Community
  • 1
  • 1
Lingviston
  • 5,479
  • 5
  • 35
  • 67
0

I was experiencing the same problem when displaying a String containing html formatted text.

Removing all html tags with myText.replaceAll("\\<.*?>","") solved the problem.

You can also remove html tags using Html.fromHtml(myText).toString(), but notice that you must use .toString() and not apply the fromHtml output directly to the TextView. If some special characters are html encoded (&amp, &gt, &lt...) you can safely apply again Html.fromHtml() to the final String.

franmontiel
  • 1,860
  • 1
  • 16
  • 20
-1

i used :

if (myModel.mDataText().length() > 150) {
    mTextView.setText(Html.fromHtml(myModel.mDataText().substring(0, 150) + "..."));
} else {
    mTextView.setText(Html.fromHtml(myModel.mDataText() + "..."));
}