Search code examples
androidtextview

TextView: Limit text to specific length with 3 dots


I have TextView which should accept max 6 characters. If value has more than 6, I want to show 3 dots at the end. Used these attributes to achieve that but it just simply cut text at the end without 3 dots.

 android:textAlignment="viewEnd"
 android:maxLines="1"
 android:maxLength="6"
 android:ellipsize="end"

Solution

  • Cleanest and quickest way would be to do so programatically as otherwise you'd need to do hard calculations of the text size + font alongside the TextView's width

    This is the cheapest

    textView.text = if(text.length > 6) text.take(6).append("…") else text