34

I want to have a single lined TextView to show up 3 dots at the end when the text is longer than the TextView. I don't know why - but I don't get it.

I already wrapped my head around similar StackOverflow questions, but I ended up with no solution. Maybe someone has some useful hints.

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:orientation="vertical">

    <TextView 
        android:textStyle="bold" 
        android:text="Full Name" 
        android:layout_height="wrap_content" 
        android:textSize="16sp"
        android:layout_width="wrap_content" 
        android:id="@+id/lName"
        android:layout_gravity="center_vertical" 
        android:maxLines="1"
        android:ellipsize="end"/>
</LinearLayout>

The LinearLayout above is nested into 2 other LinearLayouts. Maybe this is important to know. I already tried the attribute "singleLine" too, but some say this is deprecated and it doesnt work anyway.

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Bins Ich
  • 1,822
  • 6
  • 33
  • 47

8 Answers8

54

Add the following styles in your styles file (typically styles.xml):

<style name="autoscroll">
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">marquee</item>
    <item name="android:marqueeRepeatLimit">marquee_forever</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:scrollHorizontally">true</item>
</style>

Then add the style @style/autoscroll to your TextView:

<TextView android:id="@+id/lName"
      style="@style/autoscroll" />

You can reuse your autoscroll feature easily when you want this way.

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
Stefano Ortisi
  • 5,288
  • 3
  • 33
  • 41
24

Add this in your xml for the TextView:

        android:maxWidth="200dp" 
        android:maxLines="1" 

As

        android:singleLine="true"  

is deprecated.

duggu
  • 37,851
  • 12
  • 116
  • 113
Khan
  • 7,585
  • 3
  • 27
  • 44
  • Using **maxWidth** is not an *extensible* solution, and **singleLine** is now deprecated. Use **ellipsize** & **scrollHorizontally** instead. – Mickäel A. Jan 25 '13 at 14:12
  • 14
    I think there's been a misunderstanding. I just checked `TextView` at Android dev and `singleLine` is not deprecated. – Sufian Apr 29 '13 at 11:08
  • 1
    I have just tried on Simulator with android 8.0.0, and `ellipsize` doesn't work without `singleLine`, even though Android Studio marks `singleLine` as deprecated! How can this happen? – Denys Kniazhev-Support Ukraine Mar 30 '18 at 14:55
  • You can use android:maxLines = ''1" instead of singleLine as its deprecated. – Khan Mar 31 '18 at 09:45
9
android:id="@+id/lName" android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="Avinljhakjhsajkhakjshda"
    android:textSize="16sp"
LuminiousAndroid
  • 1,557
  • 4
  • 18
  • 28
  • 7
    `ellipsize` is NOT deprecated. Which website do you use for reference, I just checked Android dev. – Sufian Apr 29 '13 at 11:11
  • It's a mistake, I was talking about **singleLine** not **ellipsize**. Thanks for correcting it. – Mickäel A. Apr 29 '13 at 13:44
  • What makes you say singleLine would be deprecated? – rds Jul 23 '14 at 13:34
  • From the http://developer.android.com/reference/android/R.attr.html#singleLine : This constant was deprecated in API level 3. This attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine). – AshleyJ Aug 21 '14 at 08:15
5

This doesn't work with me unless adding 'android:ems' attribute along with the android:ellipsize attribute

android:ellipsize="end"
android:singleLine="true"
android:ems="8"
Zain
  • 37,492
  • 7
  • 60
  • 84
  • 4
    I think it's not because of setting `android:ems` specifically. If we want Android to draw ellipses, then we have to specify the length of the `TextView`, i.e. the width must not be `wrap_content`. – rickchristie May 24 '21 at 15:44
3

The ellipses did not appear while my text was selectable. I needed to disable selectable text (which is the default):

android:layout_width="0dp" (match constraint)
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textIsSelectable="false"
1

It works with singleLine="true" but this attribute is now deprecated, use ellipsize and scrollHorizontally="true" instead.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
  • 2
    You've posted it a couple of times, `ellipsize` and/or `singleLine` being deprecated. Kindly share the documentation where it says so. As I have said previously, I checked Android dev. – Sufian Apr 29 '13 at 11:13
  • As I said in my post **singleLine** is deprecated since some time now, just use **ellipsize** combined with `scrollHorizontally=true`, as none of those 2 are deprecated, and it should work. – Mickäel A. Apr 29 '13 at 13:41
  • Well, here [it's not deprecated](http://developer.android.com/reference/android/widget/TextView.html#setSingleLine(boolean)). – Sufian Apr 30 '13 at 04:45
  • 1
    Yes it is because the *method* is not deprecated, no other one came to replace it as there is no reason to, but the *attribute* is. You can see it [here](http://developer.android.com/reference/android/R.attr.html#singleLine). – Mickäel A. Apr 30 '13 at 20:40
  • It's weird that we can use its setter while the property/attribute is deprecated. – Sufian May 01 '13 at 04:17
  • Probably because **singleLine** is deprecated as he had been replaced by another attribute doing the same thing but in a better way. This is not the case of it's setter : if you want to access the **singleLine** attribute you still use the *setSingleLine()* function, it's hasn't been replaced by another one. But yeah anyway it's still weird ! – Mickäel A. May 01 '13 at 19:02
  • 1
    I totally back @Flawyte on this one. I have noticed more than once AndroidStudio in canary channel displaying singleLine as 'deprecated' in xml as well. and 'maxLines' is the correct TAG to use instead. – sud007 Apr 12 '16 at 13:48
0

Use this code it will definitely work.

<TextView
    android:id="@+id/sAddress"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:ellipsize="end"
    android:text="Adress"
    android:textSize="14sp"/>
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

Okay this way worked for me.

I am using the constraint layout and forgot to add the constraint at the end after adding the constraints I can able to see the ellipsis at the end of the textview

<androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/txtName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        android:text="asdasdnadnlaskldaklskdlasnkldsnkla"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        style="@style/TextStyle"
        android:ellipsize="end"
        android:maxLines="1"
        android:textStyle="bold"
        android:textColor="@color/black"
        />