Android Replace ... with ellipsis character
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Android development, replacing three consecutive dots (...) with the single Unicode ellipsis character (\u2026) is a small detail that improves typographic quality and simplifies character counting. The ellipsis occupies one character position instead of three, which matters for database column limits, SMS messages, and any context where string length is constrained.
This article covers three practical approaches: direct string replacement in Java and Kotlin, the built-in XML ellipsize attribute for truncation, and a SpannableString technique for fine-grained styling.
Approach 1 -- String.replace in Java and Kotlin
The simplest solution is a one-liner using String.replace. This works on any String value regardless of where it came from (user input, API response, resource file).
Java
Kotlin
A few things to keep in mind. String.replace replaces every occurrence in the string, so a value like "one...two...three" becomes "one\u2026two\u2026three". If you only want to replace a trailing occurrence, use replaceFirst with a regex that anchors to the end:
Approach 2 -- XML ellipsize Attribute
When you want a TextView to automatically truncate long text with an ellipsis, Android provides the android:ellipsize attribute. This does not replace literal dots in your string; instead, it instructs the view to clip overflowing text and append the ellipsis character at the specified position.
The ellipsize attribute accepts four values:
| Value | Behavior |
start | Clips the beginning of the text |
middle | Clips the middle of the text |
end | Clips the end of the text |
marquee | Scrolls the text horizontally |
You must also set android:maxLines (or android:singleLine="true" for the deprecated API) so the view knows when to trigger truncation.
Approach 3 -- SpannableString for Custom Styling
If you need the ellipsis character to have different styling from the rest of the text -- for example, a lighter color or a smaller font size -- use a SpannableString:
This technique gives you full control over how the ellipsis is rendered without affecting the rest of the string.
Why Use the Unicode Ellipsis
You might wonder whether it matters. Here are concrete reasons to prefer \u2026 over three dots:
- Correct character count.
"Hello\u2026".lengthis 6, while"Hello...".lengthis 8. This affects validation logic, database storage, and UI layout calculations. - Typography. Fonts often kern the three dots in an ellipsis differently from three standalone period characters, producing a more polished look.
- Accessibility. Screen readers may interpret
\u2026as "ellipsis" and read it once, whereas...can be read as "dot dot dot."
Common Pitfalls
- Replacing dots that are not ellipses. A string like
"version 3.2.1"contains consecutive dots that should not become an ellipsis. Anchor your replacement pattern or pre-validate context before replacing. - Forgetting
maxLineswithellipsize. Without a line limit, theTextViewsimply wraps and never triggers truncation, so no ellipsis appears. - Double-encoding in JSON or XML. If your string source already contains the Unicode escape
\u2026, runningreplace("...", "\u2026")will not match and leaves the original dots intact. Check the actual character values at runtime. - Locale-specific punctuation. Some languages use different continuation markers (e.g., Chinese uses
......-- six dots). Blindly replacing three dots can break localized content. - Mixing
ellipsizewithandroid:bufferType="spannable". SettingbufferTypetospannablecan interfere with the built-in ellipsize behavior on certain Android versions. Test on your minimum supported API level.
Summary
- Use
String.replace("...", "\u2026")in Java or Kotlin for a quick, universal replacement of three dots with the Unicode ellipsis character. - Use the
android:ellipsizeXML attribute when you want the system to automatically truncate overflowingTextViewcontent. - Use
SpannableStringwhen the ellipsis needs distinct styling such as a different color or font size. - The Unicode ellipsis counts as one character instead of three, which matters for length validation, storage, and accessibility.
- Always verify that your replacement logic does not accidentally convert dots that are not meant to be ellipses, such as version numbers or decimal points.

