386

I have a TextView that I want to limit characters of it. Actually, I can do this but the thing that I'm looking for is how to add three dots (...) at the end of string. This one shows the text has continue. This is my XML but there is no dots although it limit my text.

<TextView 
        android:id                      = "@+id/tvFixture"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_toLeftOf         = "@id/ivFixture_Guest"
        android:text                    = "@string/test_06"
        android:lines                   = "1"
        android:ems                     = "3"
        android:gravity                 = "right"
        style                           = "@style/simpletopic.black" 
        android:ellipsize="end"/>
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
Hesam
  • 52,260
  • 74
  • 224
  • 365

25 Answers25

848

Deprecated:

Add one more property android:singleLine="true" in your Textview

Updated:

android:ellipsize="end" 
android:maxLines="1"
Floern
  • 33,559
  • 24
  • 104
  • 119
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • 3
    Definitely need singleLine when I just attempted this. Even though it says deprecated. – EGHDK Aug 20 '13 at 17:37
  • 55
    android:singleLine="true" is deprecated and has bad side effects. use android:ellipsize="end" and maxLine="1" instead – Hamidreza Hosseinkhani Dec 06 '14 at 08:17
  • 2
    What are the "bad side effects" of using `singleLine`? See also: [Is the xml attribute singleLine deprecated or not in Android?](https://stackoverflow.com/questions/30028697/is-the-xml-attribute-singleline-deprecated-or-not-in-android) – Suragch Jul 24 '17 at 05:40
  • 3
    btw, android:maxLines can be greater than 1 if you want :) – Touré Holder Nov 23 '17 at 07:35
  • 1
    Has anyone got this msg " W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1" >? – COLD ICE Mar 26 '18 at 16:09
  • @COLDICE I just started seeing this pop up, I wonder if it came in with the new update. Seems like a bug. – Dan 0 Mar 31 '18 at 01:58
  • when you use deprecated 'singleLine' the hint says you should use 'maxLines' instead, but there is not a single word about 'ellipsize'. Ahhrr! Was it so hard to add a couple of words of how to make a simple TextView single lined? Why should we check Stackoverflow for such a trivial thing? – Kirill Karmazin Aug 24 '18 at 13:14
  • you can use android:lines="1" too – codegames Jun 22 '19 at 00:29
  • Sometimes you might need app:layout_constrainedWidth="true" also – Dan Crisan Feb 14 '20 at 11:02
  • If i use maxLines=1, the text sometimes truncates after 10 characters, sometimes 15, sometimes more or less. So, I used singleLine=true and that solved this issue. – Tayyab Mazhar Mar 08 '20 at 13:46
  • If singleLine is deprecated, then why is it still in the android documentation? Without any mention of deprecation? https://developer.android.com/reference/android/widget/TextView#attr_android:singleLine – Danny E.K. van der Kolk Jan 21 '21 at 00:02
  • For me it only works with `singleLine` although it's deprecated. Weird – mathematics-and-caffeine Nov 03 '21 at 16:51
184

The following is what I learned by playing around with various options for forcing a TextView to a single line (with and without the three dots).

enter image description here

android:maxLines="1"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="one two three four five six seven eight nine ten" />

This just forces the text to one line. Any extra text is hidden.

Related:

ellipsize="end"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:ellipsize="end"
    android:text="one two three four five six seven eight nine ten" />

This cuts off the text that doesn't fit but lets users know that the text has been truncated by adding an ellipsis (the three dots).

Related:

ellipsize="marquee"

<TextView
    android:id="@+id/MarqueeText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="one two three four five six seven eight nine ten" />

This makes the text scroll automatically across the TextView. Note that sometimes it needs to be set in code:

textView.setSelected(true);

Supposedly android:maxLines="1" and android:singleLine="true" should do basically the same thing and since singleLine is apparently deprecated I would prefer not to use it, but when I take it out, the marquee doesn't scroll anymore. Taking maxLines out doesn't affect it, though.

Related:

HorizontalScrollView with scrollHorizontally

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/horizontalScrollView">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:text="one two three four five six seven eight nine ten" />
</HorizontalScrollView>

This allows the user to manually scroll to see the whole line of text.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • But this is just ellipsizing at the end of every word. Is there any way to add ... in the middle of a very_long_word_without_whitespace... ? – Tobias Reich Mar 08 '17 at 10:45
  • @TobiasReich, I'm not aware of a built in way to accomplish that. You could make your own using [Paint.measureText](https://developer.android.com/reference/android/graphics/Paint.html#measureText(java.lang.String)). – Suragch Mar 08 '17 at 10:49
  • Sometimes you might need app:layout_constrainedWidth="true" also – Dan Crisan Feb 14 '20 at 11:03
89

Try this property of TextView in your layout file..

android:ellipsize="end"
android:maxLines="1"
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
32

I take it you want to limit width to one line and not limit it by character? Since singleLine is deprecated, you could try using the following together:

android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
Muz
  • 5,866
  • 3
  • 47
  • 65
17

eg. you can use

android:maxLength="13"

this will restrict texview length to 13 but problem is if you try to add 3 dots(...), it wont display it, as it will be part of textview length.

     String userName;
     if (data.length() >= 13) {
            userName = data.substring(0, 13)+ "...";

     } else {

            userName = data;

    }
        textView.setText(userName);

apart from this you have to use

 android:maxLines="1"
Nayanesh Gupte
  • 2,735
  • 25
  • 37
  • 2
    You should better use "\u2026" instead of "..." – Zharro Jan 14 '15 at 08:59
  • this was just as an example. if I'm putting strings in Strings.xml then definitely I go for unicodes. Any specific reason you want to put here ? – Nayanesh Gupte Jan 14 '15 at 10:34
  • 2
    Nope. I personally have recently learned of the existence of this character in the Unicode and decided to share this knowledge ;) – Zharro Jan 14 '15 at 11:09
12

Use

  • android:singleLine="true"
  • android:maxLines="1"
  • app:layout_constrainedWidth="true"

It's how my full TextView looks:

    <TextView
    android:id="@+id/message_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:maxLines="1"
    android:singleLine="true"
    android:text="NAME PLACEHOLDER MORE Text"
    android:textColor="@android:color/black"
    android:textSize="16sp"

    android:textStyle="bold"
    app:layout_constrainedWidth="true"
    app:layout_constraintEnd_toStartOf="@id/message_check_sign"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintStart_toEndOf="@id/img_chat_contact"
    app:layout_constraintTop_toTopOf="@id/img_chat_contact" />

Picture of <code>TextView</code>

AVK
  • 3,893
  • 1
  • 22
  • 31
kyrylo
  • 121
  • 1
  • 4
8

Steps to add '...' at end of the text if it is too long:

  1. check that the text width is constant
  2. add these two lines android:ellipsize="end" android:maxLines="1"

Complete code of textview in constraint layout:

<TextView
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="15sp"
        android:textAllCaps="false"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent" />
  • 1
    Hi, thanks for your answer. Multiple other answer such as [this one](https://stackoverflow.com/a/48319801/10952503) or [this one](https://stackoverflow.com/a/30029722/10952503) already explain everything that you say, so when you will have enough reputation, you will be able to upvote them instead of making duplicate – Elikill58 Aug 31 '21 at 07:59
  • android:ellipsize="end" and specially android:maxLines="2" it turned out nice. Thanks – serif Apr 06 '22 at 15:20
6

I am using Horizonal Recyclerview.
1) Here in CardView, TextView gets distorted vertically when using

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

Check the bold TextViews Wyman Group, Jaskolski... enter image description here

2) But when I used singleLine along with ellipsize -

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

Check the bold TextViews Wyman Group, Jaskolski... enter image description here

2nd solution worked for me properly (using singleLine). Also I have tested in OS version: 4.1 and above (till 8.0), it's working fine without any crashes.

Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
5

code:

TextView your_text_view = (TextView) findViewById(R.id.your_id_textview);
your_text_view.setEllipsize(TextUtils.TruncateAt.END);

xml:

android:maxLines = "5"

e.g.

In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know the mysteries of the kingdom of heaven, but to them it has not been given.

Output: In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know...

ruin3936
  • 535
  • 4
  • 13
4

You can limit your textview's number of characters and add (...) after the text. Suppose You need to show 5 letters only and thereafter you need to show (...), Just do the following :

String YourString = "abcdefghijk";

if(YourString.length()>5){

YourString  =  YourString.substring(0,4)+"...";

your_text_view.setText(YourString);
}else{

 your_text_view.setText(YourString); //Dont do any change

}

a little hack ^_^. Though its not a good solution. But a work around which worked for me :D

EDIT: I have added check for less character as per your limited no. of characters.

Zahra.HY
  • 1,684
  • 1
  • 15
  • 25
TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
  • [It's already explained here](http://stackoverflow.com/questions/10748796/android-how-to-limit-width-of-textview-and-putting-three-dots-at-the-end-of-te/16580374#16580374) – Straw Hat Jan 15 '14 at 16:41
4

I got the desired result by using

android:maxLines="2"
android:minLines="2"
android:ellipsize="end"

The trick is set maxLines and minLines to the same value... and Not just android:lines = "2", dosen't do the trick. Also you are avoiding any deprecated attributes.

user2275344
  • 39
  • 3
  • 12
user1974433
  • 67
  • 1
  • 3
4

You need to add following lines into your layout for the textview

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

Hope this works for you.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
4

In order to work with the android:ellipsize attribute, you have to limit the layout width of the TextView, such that the text is out of bounds from view of TextView.

So, android:layout_width attribute plays a key role here, set it accordingly.

One example can be:

<TextView        
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:text="This is a very long text to be displayed"
    android:textSize="12sp"
    android:maxLines="1"            
     />

Now, here if the text in android:text="This is a very long text to be displayed" goes out of view from TextView with a android:layout_width="120dp", android:ellipsize="end" will truncate the text and place ...(3 dots) after it. i.e. This is very long... will be displayed in the TextView.

Androwed
  • 443
  • 5
  • 10
4
        <TextView
            android:id="@+id/product_description"
            android:layout_width="165dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:text="Pack of 4 summer printed pajama"
            android:textColor="#d2131c"
            android:textSize="12sp"
            android:maxLines="2"
            android:ellipsize="end"/>
3

I think you give fix height and width of text view. Then your solution will work.

3

you can write this line in xml where you take the textview :

android:singleLine="true"
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Jay Thakkar
  • 743
  • 1
  • 5
  • 24
2

The approach of @AzharShaikh works fine.

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

But I realize a trouble that TextView will be truncated by word (in default). Show if we have a text like:

test long_line_without_any_space_abcdefgh

the TextView will display:

test...

And I found solution to handle this trouble, replace spaces with the unicode no-break space character, it makes TextView wrap on characters instead of words:

yourString.replace(" ", "\u00A0");

The result:

test long_line_without_any_space_abc...

Phong Nguyen
  • 6,897
  • 2
  • 26
  • 36
2

Apart from

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

you should set

android:layout_width="0dp"

also know as "match constraint", because the wrap_content value just expands the box to fit the whole text, and the ellipsize property can't make its effect.

Nekomajin42
  • 638
  • 1
  • 6
  • 20
2

Simple for three dots

android:layout_width="100dp" <!--your dp or match_parent or 0dp>
android:maxLines="2" <!--count your line>
android:ellipsize="end"
Fortran
  • 2,218
  • 2
  • 27
  • 33
1

Add These two lines in your text

android:ellipsize="end"
android:singleLine="true"
SANJAY GUPTA
  • 1,564
  • 13
  • 21
0

you can do that by xml:

<TextView 
    android:id="@+id/textview"
    android:maxLines="1" // or any number of lines you want
    android:ellipsize="end"
    />
Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
0

set android:maxLength="8" in Textview

if you want to set yourString >5. then set textview length 5+3 (for three-dot)

if (yourString.length()>5) // 
    {
        textview.setText(yourString.substring(0,5)+"...");
    }
    else {
        textview.setText(title);
    }
Rahul Singh
  • 168
  • 2
  • 6
0

1.set static width like 7odp

2.use android:ellipsize="end"

3.use android:maxLines="1"

4.use android:singleLine="true"

<TextView
        android:id="@+id/tv_status"
        **android:layout_width="70dp"**
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/padding_8"
        android:gravity="center|center_horizontal"
        android:includeFontPadding="false"
        android:textColor="@color/black_2a"
        android:textSize="@dimen/text_size_1"
        **android:ellipsize="end"
        android:maxLines="1"
        android:singleLine="true"**
        app:layout_constrainedWidth="true"
        app:borrowStatusText="@{item.lenders[0].status}"
        app:layout_constraintEnd_toStartOf="@id/iv_vector"
        app:layout_constraintTop_toTopOf="parent" />
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

In this way you can set the maximum length of the view and, at the same time, you will have dots at the end ("num" is your number of dp):

android:maxWidth="{num}dp"
android:ellipsize="end"
Kontick
  • 85
  • 6
-1

You just change ...

android:layout_width="wrap_content"

Use this below line

android:layout_width="match_parent"

.......

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="10dp"
       android:layout_toRightOf="@+id/visitBox"
       android:orientation="vertical" >

             <TextView
                  android:id="@+id/txvrequestTitle"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:singleLine="true"
                  android:text="Abcdefghighiklmnon"
                  android:textAppearance="? 
                  android:attr/textAppearanceLarge"
                  android:textColor="@color/orange_color" />

   </LinearLayout>
Kabir
  • 852
  • 7
  • 11
KeTaN
  • 724
  • 8
  • 22