Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

How do I Auto Resize TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

How do I limit characters in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.

What is Ellipsize?

Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.


You should be able to use the "ellipsize" property of a text view:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_mytext"
    android:ellipsize="end"
    android:maxLines="1"
/>

You may also need to apply gravity values to the layout too; I have sometimes seen "auto-stretching" views without them.


Found an interesting work-a-round for this problem.

maxLines=1
ellipsize=end
scrollHorizontally=true

The trick is that last statement about horizontal scrolling .... check it out. It at least works on v2.2.


Programmatically, you can use:

TextView tx = new TextView(this);
tx.setTextSize(13);
tx.setGravity(Gravity.CENTER);
tx.setTop(90);
tx.setText("Long text here");
tx.setTextColor(Color.BLACK);
tx.setSingleLine(true);
tx.setEllipsize(TruncateAt.END);