Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"/> 
like image 910
Hesam Avatar asked May 25 '12 05:05

Hesam


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 limit text 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.

How do you show bullet points on android?

The two Button s at the bottom have android:text="◄" and "►" . Show activity on this post.

How do you add multiple lines in TextView?

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.


2 Answers

Deprecated:

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

Updated:

android:ellipsize="end"  android:maxLines="1" 
like image 119
Mohammed Azharuddin Shaikh Avatar answered Oct 02 '22 02:10

Mohammed Azharuddin Shaikh


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).

enter image description here

android:maxLines="1"

<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:

  • android:maxLines
  • android:singleLine (Note this and this)
  • android:lines

ellipsize="end"

<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:

  • ellipsize="start" (...aaabbbccc)
  • ellipsize="middle" (aaa...ccc)
  • android: Ellipsise , meaning of the options

ellipsize="marquee"

<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:

  • Marquee text in Android

HorizontalScrollView with scrollHorizontally

<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.

like image 39
Suragch Avatar answered Oct 02 '22 00:10

Suragch