4

I have a TextView with a height depending on previous content, but there might be a long Text in it. How can I cut it off at the right point and concatenate three dots or something similar to the new end?

Currently it looks like this: Multiline text which is cut off

I found some solutions for Single Line Text, but how does it work with more than one line? I also do not know the number of lines, because this depends on the screen size.

Are there other typical ways on Android to show that the text can be extended? E.g. a colour gradient in the last line?

Edit:

When I do it without a fixed heigth, I have to make the height depend on the element above and my XML will look like this:

<TextView
   android:id="@+id/podcastShortDesc"
   android:text="Long text"
   android:layout_below="@+id/podcastTitle"
   android:layout_toRightOf="@+id/podcastLogo"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"

   android:maxLines="3"
   android:ellipsize="end"

   android:layout_above="@+id/podcastMoreAction" />

When I do specify maxLines I can have luck an it will work: I have luck

But if the title is too big, it does not work:

enter image description here

Eknoes
  • 508
  • 1
  • 11
  • 24

3 Answers3

3

You should add following code for "3 dots" at the end.

android:ellipsize="end"

You should remove fixed height property

android:layout_height="50dip"

instead you should add number of lines

android:maxLines="4"  
android:layout_height="wrap_content"  

Android will take care everything else. In this way, even if text is smaller than 4 lines, android will take care size. If it is more than 4 lines, it will add "3 dots" :) Specify fixed height may cut your text.

mpals
  • 251
  • 1
  • 6
  • 1
    Thanks! But unfortunately I don't know the maxLines, this depends on the previous content. I just updated my question to explain this. – Eknoes May 04 '16 at 17:21
0

Try this. Hope it will work.

<TextView
android:id="@+id/podcastShortDesc"
android:text="LONG LONG LONG TEXT"
android:layout_width="match_parent"
android:layout_height="50dip"
android:maxHeight="50dp"
android:ellipsize="end"/>

I have tested 50dp can show two line in normal font size. So in 50dp height you should add maxLines 2.

Masum
  • 4,879
  • 2
  • 23
  • 28
  • The problem is, that I do not know how many lines I have - this will be different. So I do not want to specify a maximum number of lines. – Eknoes May 04 '16 at 16:58
  • If you don't know the height. you should add maxHeight. If you add maxHeight it automatically add vertical scroll bar. – Masum May 04 '16 at 17:02
  • I just updated my question, maybe my problem is now easier to understand – Eknoes May 04 '16 at 17:21
0

Try this fix layout_height and add scroll in your textview

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="50dip"
android:text="Hello......"
android:scrollbars="vertical"/>
omkar ugale
  • 106
  • 7