TextView Ellipsize (...) not working
Asked Answered
F

8

35

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.

Firstfoot answered 29/6, 2012 at 12:36 Comment(0)
L
56

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.

Leafy answered 29/6, 2012 at 12:43 Comment(4)
But there is no Style value for TextViews!?Planksheer
use style="@style/autoscroll" instead android:style="@style/autoscroll"Supergalaxy
how to can i use as programicallyGyroscope
In addition to the <style ...>...</style> definition above, I had to programmatically also call setSelected(true) on the TextView in order to allow it to scroll. Without this, the TextView always behaved like android:ellipsize="end" instead of "marquee".Hideandseek
R
24

Add this in your xml for the TextView:

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

As

        android:singleLine="true"  

is deprecated.

Return answered 29/6, 2012 at 13:2 Comment(4)
Using maxWidth is not an extensible solution, and singleLine is now deprecated. Use ellipsize & scrollHorizontally instead.Irrecusable
I think there's been a misunderstanding. I just checked TextView at Android dev and singleLine is not deprecated.Mossy
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?Gayl
You can use android:maxLines = ''1" instead of singleLine as its deprecated.Return
G
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"
Goring answered 29/6, 2012 at 13:10 Comment(4)
ellipsize is NOT deprecated. Which website do you use for reference, I just checked Android dev.Mossy
It's a mistake, I was talking about singleLine not ellipsize. Thanks for correcting it.Irrecusable
What makes you say singleLine would be deprecated?Gabriellagabrielle
From the 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).Silversmith
C
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"
Cowes answered 4/8, 2019 at 23:22 Comment(1)
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.Greatcoat
C
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"
Christophe answered 5/1, 2020 at 1:16 Comment(0)
T
1

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

Tanguy answered 25/1, 2013 at 14:14 Comment(7)
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.Mossy
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.Irrecusable
Well, here it's not deprecated.Mossy
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.Irrecusable
It's weird that we can use its setter while the property/attribute is deprecated.Mossy
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 !Irrecusable
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.Overcritical
S
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"/>
Salmonella answered 21/1, 2021 at 11:13 Comment(0)
M
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"
        />
Mako answered 14/5, 2023 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.