Android: TextView automatically truncate and replace last 3 char of String

Viewed 100393

If a String is longer than the TextView's width it automatically wraps onto the next line. I can avoid this by using android:singleLine (deprecated) or by setting android:inputType="text". What I need now is something that replaces the last 3 characters of my String with "...". Since I'm not using a monospace font this will always be different depending on the letters used in my String. So I'm wondering what's the best way to get the last 3 characters of a String in a TextView and replace them. Maybe there's already something implemented in the Android framework, since this must be a common problem.

4 Answers

Just add

<TextView
    android:id="@+id/newsTitleTextView"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@id/newsThumbnailImageView"
    android:ellipsize="end"
    android:maxLines="2"
    android:text="Analysts See Slower Bitcoin Hashrate Growth in 2022 Amid Market Correction "
    android:textSize="15sp" />
Related