5

I'm working on a TextView which is contained in a ConstraintLayout.

I want ellipsize to add three dots at the end of text(in the TextView) if its length exceeds maxLength.

maxLines="1" and ellipsize="end" seemed to the best answer after a thorough research about my question. Unfortunately, it didn't work for my case. The three dots did't show at all.

Here's the snapshot :

enter image description here

The original string was "Test for long description"(The n was dropped). It's supposed to show "Test for long descrip...".

Here's my xml :

<TextView
    android:id="@+id/txtDescription"
    android:maxLength="24"
    android:maxLines="1"
    android:ellipsize="end"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="120dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    android:text="DescriptionView"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.72" />

I feel like there's something override ellipsize in the XML, or did I miss something crucial in the XML file?

3
  • You posted image is not for the xml file
    – navylover
    Commented Nov 17, 2018 at 8:17
  • @navylover The xml is for the description TextView in the image. The image is for the whole Constraintlayout.
    – Jerry Chen
    Commented Nov 17, 2018 at 8:35
  • improved formating and grammar
    – Sagar Zala
    Commented Nov 17, 2018 at 12:05

6 Answers 6

9

The problem is:

android:maxLength="24"

remove it since you want the TextView to be ellipsized.
A TextView gets ellipsized when it is not wide enough to show the whole text.
I think you do not understand what this attribute android:ellipsize="end" is all about.
If it is wide enough then you will not see any dots at the end because they are not needed.
If you set android:layout_width="40dp" then you will see the dots.
With android:layout_width="wrap_content" the TextView is wide enough and it is not ellipsized.

3
  • 1
    Not working. The only change is it shows the entire string, but not ellipsized at all.
    – Jerry Chen
    Commented Nov 17, 2018 at 8:37
  • 1
    If it shows the whole string this means that it is wide enough to show the whole string.
    – forpas
    Commented Nov 17, 2018 at 8:42
  • Yes, you're right! Everything goes as what I want after setting the TextView width to a particular value!
    – Jerry Chen
    Commented Nov 17, 2018 at 11:43
4

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

(But its Deprecated so use second option)

2) Use this three together

android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
3
  • It's not working, even scrollHorizontally can't scroll, but thanks anyway.
    – Jerry Chen
    Commented Nov 17, 2018 at 8:43
  • have you set fixed width for EditText ? Commented Nov 17, 2018 at 9:01
  • android:singleLine="true" is deprecated,use android:maxLines= "1" instead
    – iCantC
    Commented Apr 5, 2019 at 9:46
1

Ellipsize is quite a pain in android, apply below on your textView :

txtDescription.setSingleLine(true);

Hope it helps.

3
  • No , it's not working. It's still the same, but thanks anyway.
    – Jerry Chen
    Commented Nov 17, 2018 at 8:41
  • play with layout width, try giving particular value.
    – Sahil
    Commented Nov 17, 2018 at 8:46
  • YES! It works. It's finally ellipsized! Thanks a lot!
    – Jerry Chen
    Commented Nov 17, 2018 at 11:39
0

The key for making three dots shows is constrain the width of the text view. Suppose the name of image in your posted figure is left_image, change the xml attributes of TextView as below:

....
android:layout_width="0dp" 
app:layout_constraintStart_toEndOf="@+id/left_image"
....

above two lines make the width of TextView is constrainted(between left img and right border of parent).

1
  • I set the TextView width to a particular value, it works! Thanks!
    – Jerry Chen
    Commented Nov 17, 2018 at 11:42
0

In my case following line was the problem. I commented it out and it worked.

<item name="android:inputType">text|textNoSuggestions</item>
0

To fix this, you need to use a fixed width for the textview instead of maxLength attribute in your xml In xml :

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

And in java/kotlin program :[Important]

textDescription.setSelected(true) -> Java

or

textDescription.isSelected=true -> Kotlin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.