Android, How to Limit Width of Textview (And Add Three Dots at The End of Text)

Android, How to limit width of TextView (and add three dots at the end of text)?

Deprecated:

Add one more property android:singleLine="true" in your Textview

Updated:

android:ellipsize="end" 
android:maxLines="1"

Display three dots at the end of the textview

Add a drawablePadding attribute along with maxLines and ellipsize.

Try this,

    <EditText
android:id="@+id/etZipCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="8"
android:text="3445"
android:padding="15dp"
android:singleLine="true"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:drawableRight="@drawable/ic_dropdown_pin"
android:minHeight="50dp"
android:padding="15dp"
android:text="Alaska Standard Time"
android:ellipsize="end"
android:maxLines="1"
android:drawablePadding="10dp"
android:singleLine="true"/>

</LinearLayout>

Screenshot below,

Screenshot

How to show three dots at the end of text view

You're using ConstraintLayout inside CardView and I can see that you've constrained the TextView properly but its width is wrap_content which what causing the issue. set it to 0dp and your android:ellipsize and android:singleLine attributes will work.

Try this:

<TextView
...
android:layout_width="0dp"
... />

TextView: Limit text to specific length with 3 dots

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

How to show 3 dots at the end of text in textview

Make a preset width for your TextView and add these attributes (Shout-out to @T-D)

android:ellipsize="end"
android:maxLines="1"

The maxLines will make sure that your text wont be shown in two lines and the ellipsize will add the three dots in the end.

Old Answer

android:ellipsize="end"
android:singleLine="true"

putting three dots if TextSize is more in android

put this code to your TextView

android:ellipsize="end"
android:singleLine="true" // if you want 3 dots for single line, add this.
android:maxLines="2" // if you want 3 dots after two lines, add this.

3 dots in the end of TextView are vertically centered

It's hard to tell without access to your full layout, but that's not the standard behavior, so you may be using a custom Font/Theme.

This is how it looks in a blank layout with a ConstraintLayout:

TextView with IDE/XML code and preview visible on the right

Notice I pinned the "end" to the parent, since I don't know what your widgetTimeArrived is.

Ellipsize not show three dots at end of TextView

I used ellipsize=true in the project I developed before, and it worked well in that project, I compared the code of the current project with the previous one. There is no difference between each other, so I am confused with the current project, so I pulled the previous project from github and ran it, and found that it NOT WORK in my xml renderer, but it WORKS on my phone.

Eventually I found it is the problem of the renderer. I found the button to upgrade the renderer. Now I use the latest renderer, and it also works fine in the redener now.



Related Topics



Leave a reply



Submit