65

I have a TextView in my layout which is wrap_content in layout_width. It is limited to maximum of 15 characters so I'm using maxLength.

I need to end this TextView with 3 dots (...) and it happens only when I give the layout_width a fixed size in dp, something that I don't want to do.

I know it is possible programmatically by trimming the string after the 15th character and then adding the 3 dots, but I prefer to do that by XML.

Any idea how to end the text with 3 dots and leave it wrap_content?

<TextView
    android:id="@+id/inbox_contactName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1"
    android:maxLines="1"
    android:ellipsize="end"
    android:singleLine="true"
    android:maxLength="15"
    android:textColor="#0670b4"
    android:textSize="16sp" />
Yaniv
  • 3,321
  • 8
  • 30
  • 45

11 Answers11

86

This will solve your problem, Use ellipsize Property in your XML Code

android:ellipsize="end" <!-- This makes the magic ... thing -->
android:maxEms="15" <!-- Limit of the Text -->
android:singleLine="true" <!-- In case if you want everything in one line -->

Edit: singleLine is deprecated. Use maxlines="1" instead.

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
Kirk
  • 4,957
  • 2
  • 32
  • 59
  • 1
    I noticed today that `singleLine="true"` and `maxlines="1"` behave differently when used with `maxWidth` and `ellipsize` - for some reason it truncates more with `maxlines` - don't know why though. – NemoOudeis Nov 30 '16 at 06:14
  • 9
    @Kirk maxEms is not working for restricting the number of digits, whereas, using maxLength, doesn't show dots. – Narendra Singh Feb 04 '17 at 11:14
  • 2
    @king possibly deprecated. At the time of answering this question, It was a working solution. If anybody knows the latest patch to this, Kindly update my answer. – Kirk Feb 04 '17 at 11:23
  • Ems != number of characters – AlexAndro May 02 '18 at 13:50
  • @AlexAndro what's that dude? – Kirk May 02 '18 at 19:23
  • @Kirk Just an info! It seems to me that some people think is the same thing. – AlexAndro May 03 '18 at 08:44
  • 1
    This doesn't solve the problem, as `maxEms` limit the width to fixed value. It will work only if you are developin app for certain resolution, which is usually never. – Firzen May 01 '19 at 15:41
  • @Firzen Answer is targetted for the OP's question, not for you. In my answer, I didn't state what EMS does or how it behaves. Also, it's called EMS, not pixel limiter. You might be expecting a different answer to your question. Hope you understand the difference as well before you downvote. – Kirk May 06 '19 at 07:58
24

You cannot use both maxLength and Ellipsize although you can define Maximum EMS see the example below

<TextView
        android:id="@+id/tv_hist_source_lang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxEms="8"
        android:maxLines="1"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium" />

It looks like this

AZ_
  • 21,688
  • 25
  • 143
  • 191
  • This is working in spite that maxLines="1" is specified because maxEms is limiting the length of string put in TextView. This kind of limitation is also specified by maxLength if it goes beyond the width of the TextView if it is specified say 150dp (then it shows the ellipsis). – Abhinav Saxena Dec 05 '21 at 15:33
18

I gather (from comment) that @Yaniv already solved it using code - but this is the right way to do it (with xml). May help other users who land here. Trick is to use both toleftof and torightof.

<RelativeLayout>
...
 <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:singleLine="true"
        android:layout_toRightOf="@id/some_element1"
        android:layout_toLeftOf="@id/some_element2"/>
...
<RelativeLayout>
Ravi
  • 3,719
  • 6
  • 28
  • 40
  • I only had to use `toLeftOf`. – theblang Jan 22 '14 at 18:56
  • It will not work until and unless layout_width is match_parent. It will also not work if there are three or more text views in a row (for example) and layout_width is set to wrap_content. Therefore you should limit the size of the text view's width by weight, max_length, lines, or ems. – Abhinav Saxena Dec 05 '21 at 15:37
14

You can use

android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"

Only this works for me.

Zhou Hongbo
  • 1,297
  • 13
  • 25
8

One of the easiest way is to add Right Padding + Ellipsize

<TextView
            android:id="@+id/txtvw_contentcell_subhead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Polem sampler, Lorem Ipsum golep tolem burop yemit noski"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@color/caption_black_color"
            android:textSize="@dimen/caption_size"
            android:paddingRight="40dp"/>

Here is an example Subtitle Text with char limit. enter image description here

Enzokie
  • 7,365
  • 6
  • 33
  • 39
7

use this android:ellipsize="end"

Mahmoud Hashim
  • 550
  • 5
  • 13
3

Tried most of the solutions above. Using maxWidth was the key for me:

android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"
John T
  • 814
  • 10
  • 17
2

Very Important: ellipsize = "end" only works when maxLength is set to a value that is more than the number of characters on one line. You can use wrap_content if you align the TextView's end and start to any other view(s).

0

I needed to do this with Radio Button and I wanted to limit the size to one line. When I used Android:singleline="true", radio button's check circle disappeared. Here's the code that finally worked:

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

There's no need to set ems. This will probably work also in TextView and other UI components. Hope this helps to someone.

Micer
  • 8,731
  • 3
  • 79
  • 73
0

You can just modify the String when it's length is above 20 to add the ellipsize.

Horatio
  • 1,695
  • 1
  • 18
  • 27
-1

remove the line from xml

android:lines="1"
        android:maxLines="1"
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57