In Android, how could I limit the width of the TextView by putting three dots at the end of the text if the length is greater to certain limit ?
Asked
Active
Viewed 3,239 times
2 Answers
10
put this code to your TextView
android:ellipsize="end"
android:singleLine="true" // if you want 3 dots for single line, add this.
android:maxLines="2" // if you want 3 dots after two lines, add this.

canova
- 3,965
- 2
- 22
- 39
-
was adding comments for explanation for each code. sorry it took some time. – canova Aug 02 '13 at 12:23
-
but how does `maxLines` affects the display of three dots at the end? – Boris Mocialov Aug 02 '13 at 12:24
-
if he has a text of 4 lines, but he wants to show the dots after 2 lines and doesn't want to show the rest of the text, this code is needed. – canova Aug 02 '13 at 12:26
-
wanted to write this information in case if he or someone will need it as well? what is the problem with this additional information? – canova Aug 02 '13 at 12:28
-
@MocialovBoris `android:layout_width="20px"` did he say about 20px width of TextView? – Pankaj Kumar Aug 02 '13 at 12:29
-
@PankajKumar he said about `limit the width of the TextView` – Boris Mocialov Aug 02 '13 at 12:29
-
Why did you started a non-sense debate here... just chill both of u – Pankaj Kumar Aug 02 '13 at 12:29
-
@MocialovBoris but where is 20px? similarly why you are debating with him... :) – Pankaj Kumar Aug 02 '13 at 12:30
-
@PankajKumar Thanks, I just gave additional information in my answer for helping and had to explain this obvious thing to MocialovBori. Interesting that this became an issue. – canova Aug 02 '13 at 12:31
1
Use the ellipsize property for this purpose.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_mytext"
android:ellipsize="end"
android:singleLine="true"
/>
The code similar to the above should work for you. Also set maxLines=1
.