How can I show ellipses on my TextView if it is greater than the 1 line?
Asked Answered
E

7

125

I have the following Layout which does not work:

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:id="@+id/experienceLayout" 
    android:background="#ffffff" 
    android:layout_height="match_parent" 
    android:paddingLeft="6dp" 
    android:paddingRight="6dp" 
    android:paddingBottom="6dp" 
    android:paddingTop="6dp">

    <TextView 
        android:layout_weight="1" 
        android:id="@+id/experienceLabel" 
        android:text="Experience" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:textStyle="bold">
    </TextView>

    <TextView 
        android:id="@+id/experienceTextView" 
        android:text="TextView" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:ellipsize="end" 
        android:lines="1" 
        android:maxLines="1" 
        android:singleLine="true" 
        android:fadeScrollbars="false">
    </TextView>

</LinearLayout>
Electrify answered 18/6, 2011 at 2:37 Comment(0)
C
339

This is a common problem. Try using the following:

android:scrollHorizontally="true"
android:ellipsize="end" 
android:maxLines="1"

.............. the scrollHorizontally is the "special sauce" that makes it work.

Closeup answered 18/6, 2011 at 3:48 Comment(7)
Strange... I tried android:scrollHorizontally="true" but it didn't work, I had to use the deprecated attribute android:singleLine="true".Hick
yes scrollHorizontally is the actual key to ...Buettner
scrollHorizontally? obviously i don't want a horizontal scroll effect.Authoritarian
@user1232726: Yes, "scroll horizontally." As a matter of common sense, take a look at the date of the question and the date of the responses ... goes without saying that neither might be currently relavant (before commenting).Closeup
Using ellipsize with maxLines might crash the app - Report from Android Studio intellisenseBerthaberthe
@VaishnavMhetre I just implement the solution above and has no crash at all.Hulbig
Works without scrollHorizontally property.Spunky
C
38

This will also make a single line with ellipsise

 android:singleLine="true"
Classicism answered 21/12, 2012 at 10:22 Comment(4)
This is deprecated apparentlyWickner
This one better breaks long words.Nicolas
android:maxLines="1"Insensibility
@grebulon It is deprecated. At least now.Marleen
E
25

Use this

android:ellipsize="end"  
android:singleLine="true"

Don't use this without fully aware of what output comes

android:ellipsize="end"  
android:maxLines="1"

When you use maxlines = 1 it will some time truncate most of the characters.

Este answered 22/2, 2016 at 15:26 Comment(0)
F
20

The way it worked for me on multiple devices / APIs was programmatically like this (where tv is your TextView):

    if (tv.getLineCount() > 1) {
        int lineEndIndex = tv.getLayout().getLineEnd(0);
        String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026";
        tv.setText(text);
    }
Ferromanganese answered 18/9, 2015 at 21:30 Comment(3)
This is the most helpful answer of all... works on every single API and can be easily convert it as a Utils library.Burnham
You should use the ellipsis character \u2026 instead of the three . charactersThumbtack
You are right @ChrisStillwell and I do use the ellipsis character in my code. I've edited the answer, thank you. :)Ferromanganese
V
4

So all the answers above cater to the requirement that only 1 line and then the ellipsis should appear. However if you want the ellipsis to appear after certain lines of text, then you should use the following:

android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"

With this the ellipsis will appear only after 2 lines. Note: Its important to have singleLine as false.

Vshaped answered 24/1, 2019 at 4:34 Comment(1)
The default value is false according to the documentation: developer.android.com/reference/android/widget/…Anatollo
H
3

This helped me out:

android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"

Make sure the TextView width is set to Match_Parent

https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518

Haematocryal answered 3/9, 2019 at 19:15 Comment(0)
P
2

android:singleLine is deprecated. In my case, I had to get a fixed height for the TextView and I used the android:lines attribute instead of android:maxLines. I thought this might help someone having the same problem as mine.

android:ellipsize="end"
android:lines="2"
Preordain answered 31/1, 2020 at 19:59 Comment(3)
Not true. Documentation doesn't say anything about deprecation: developer.android.com/reference/android/widget/…Vasectomy
@Vasectomy Interesting. This document says that it is - developer.android.com/reference/android/R.attr.html#singleLinePreordain
@cesards, According to Android in Code documentation: This (android:singleLine) attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).Sterlingsterlitamak

© 2022 - 2024 — McMap. All rights reserved.