Android
Ellipsis Character
Text Formatting
Unicode
Android Development

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

java
String original = "Loading data...";
String result = original.replace("...", "\u2026");
// result: "Loading data\u2026"

Kotlin

kotlin
val original = "Loading data..."
val result = original.replace("...", "\u2026")
// result: "Loading data\u2026"

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:

kotlin
val result = original.replaceFirst(Regex("""\.\.\.$"""), "\u2026")

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.

xml
1<TextView
2    android:id="@+id/label"
3    android:layout_width="200dp"
4    android:layout_height="wrap_content"
5    android:maxLines="1"
6    android:ellipsize="end"
7    android:text="This is a very long sentence that will be truncated" />

The ellipsize attribute accepts four values:

ValueBehavior
startClips the beginning of the text
middleClips the middle of the text
endClips the end of the text
marqueeScrolls 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:

kotlin
1import android.text.SpannableString
2import android.text.Spanned
3import android.text.style.ForegroundColorSpan
4import android.graphics.Color
5
6fun styledEllipsis(base: String): SpannableString {
7    val text = "$base\u2026"
8    val spannable = SpannableString(text)
9    spannable.setSpan(
10        ForegroundColorSpan(Color.GRAY),
11        base.length,          // start index (the ellipsis)
12        text.length,          // end index
13        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
14    )
15    return spannable
16}
17
18// Usage
19textView.text = styledEllipsis("Loading data")

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".length is 6, while "Hello...".length is 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 \u2026 as "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 maxLines with ellipsize. Without a line limit, the TextView simply 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, running replace("...", "\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 ellipsize with android:bufferType="spannable". Setting bufferType to spannable can 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:ellipsize XML attribute when you want the system to automatically truncate overflowing TextView content.
  • Use SpannableString when 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.

Course illustration
Course illustration

All Rights Reserved.