How to Add an ellipsis in a TextView
setEllipsize added in API Level 1android:scrollHorizontally added in API Level 1
An ellipsis is three periods in a row. (...)
1. Add a TextView to your activity.
2. In the main.xml file, add the below line
<TextView
....
android:text="This is a really, really, really long line of text so you can see how to ellipse works."
/>
3. In the MainActivity.java file, add the following code to the import section.
import android.text.TextUtils.TruncateAt;
4. In the MainActivity.java file, add the following code to the onCreate method.
myTextView.setEllipsize(TruncateAt.END);
5. At this point, the ellipsis will not display yet as a TextView is set to automatically expand on default when new text is entered. You will need to limit the TextView in some way. Do do this, you can use either add to your TextView a scrollHorizontally, minLines, or maxLines to have the ellipsis display.
android:scrollHorizontally = "true"
android:minLines = "2"
android:maxLines = "2"
6. To make the ellipse:
at the end use:
myTextView.setEllipsize(TruncateAt.END);
myTextView.setEllipsize(TruncateAt.MIDDLE);
myTextView.setEllipsize(TruncateAt.START);
myTextView.setEllipsize(null);
android:maxLines = "1" will not display the three dots (...)
android:lines = "1" will not display the three dots (...)
Related Articles:
- Deprecated: android:singleLine Set EditText to One Horizontally Scrolling Line
- android:maxLines - Set the Maximum Viewable Lines of text in a TextView
- android:lines - Change the number of lines of text viewable in a TextView/EditText
http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)
http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html