I have a TextView
that I want to limit characters of it. Actually, I can do this but the thing that I'm looking for is how to add three dots (...) at the end of string. This one shows the text has continue. This is my XML but there is no dots although it limit my text.
<TextView android:id = "@+id/tvFixture" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_toLeftOf = "@id/ivFixture_Guest" android:text = "@string/test_06" android:lines = "1" android:ems = "3" android:gravity = "right" style = "@style/simpletopic.black" android:ellipsize="end"/>
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!
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.
The two Button s at the bottom have android:text="◄" and "►" . Show activity on this post.
When you have a text that's longer than one line, then the TextView will automatically put the text in multiple lines. When you set the layout_width and layout_height as wrap_content or match_parent , then the TextView widget will use all the available space to display the text you specified as its content.
Deprecated:
Add one more property android:singleLine="true"
in your Textview
Updated:
android:ellipsize="end" android:maxLines="1"
The following is what I learned by playing around with various options for forcing a TextView
to a single line (with and without the three dots).
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:text="one two three four five six seven eight nine ten" />
This just forces the text to one line. Any extra text is hidden.
Related:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:ellipsize="end" android:text="one two three four five six seven eight nine ten" />
This cuts off the text that doesn't fit but lets users know that the text has been truncated by adding an ellipsis (the three dots).
Related:
<TextView android:id="@+id/MarqueeText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="one two three four five six seven eight nine ten" />
This makes the text scroll automatically across the TextView. Note that sometimes it needs to be set in code:
textView.setSelected(true);
Supposedly android:maxLines="1"
and android:singleLine="true"
should do basically the same thing and since singleLine is apparently deprecated I would prefer not to use it, but when I take it out, the marquee doesn't scroll anymore. Taking maxLines
out doesn't affect it, though.
Related:
<HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/horizontalScrollView"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:scrollHorizontally="true" android:text="one two three four five six seven eight nine ten" /> </HorizontalScrollView>
This allows the user to manually scroll to see the whole line of text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With