1

I know there are many similar questions, and I have read many answers, but none of them are what I want. I have tried almost all of them. The following are my attempts (I set ellipsize=end for each attempt):

  1. singleLine=true (works)
  2. lines=1 (not work)
  3. maxLines=1 (not work)
  4. set width of textView to specific value (not work)
  5. ScrollHorizontally=true (not work)

Only the first one works, but I want multiple lines of text instead of one line

Is there any other way to achieve this

Any help will be appreciated

Edit:

And here is my xml layout(in attempt 2):

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/x150"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:ellipsize="end"
        android:lines="1"
        android:text="hello hello hello hello hello hello hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/image" />
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Kennen
  • 41
  • 1
  • 10
  • 1
    Post your xml code – chand mohd Jul 27 '20 at 16:16
  • I have made so many attempts so I cannot post any attempt as a representative... Except for the necessary layout_height and layout_width and the constraints as a child of the constraintlayout, I did not add any other xml attributes on it – Kennen Jul 27 '20 at 16:33
  • @Kennen we ask you about whole xml of your layout. Not just TextView. – Eugene Troyanskii Jul 27 '20 at 16:42
  • Does this answer your question? 1.https://stackoverflow.com/questions/19099296/set-text-view-ellipsize-and-add-view-more-at-end. 2.https://stackoverflow.com/questions/19675331/add-view-more-at-the-end-of-textview-after-3-lines – CodeRED Innovations Jul 27 '20 at 18:39

5 Answers5

2

I used ellipsize=true in the project I developed before, and it worked well in that project, I compared the code of the current project with the previous one. There is no difference between each other, so I am confused with the current project, so I pulled the previous project from github and ran it, and found that it NOT WORK in my xml renderer, but it WORKS on my phone.

Eventually I found it is the problem of the renderer. I found the button to upgrade the renderer. Now I use the latest renderer, and it also works fine in the redener now.

Kennen
  • 41
  • 1
  • 10
0

This is working fine for me

<TextView
            android:id="@+id/textView8"
            android:layout_width="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            android:ellipsize="end"
            android:lines="3"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:text="TextVie mbdfdsvffmnsvfbnsddfdfvfndvfnbdvfbndsvfnsdvfnsdvfnsdvfnsbfjnbdsvjfvdsjhfvsdjhvsdsdvfjdvfjsdvfjhsdvfjsdvfjdhsvfhjdvfjdvfhjdvfjdsvfjsvfhjsdvfjsdvfjsdvfjsdvfjsdvfnsdfvbnsdvfbnsdvfnsdvfnsdbvfnbsdvnsdvsdvbfsdnbfw"
            android:textSize="16dp" />
chand mohd
  • 2,363
  • 1
  • 14
  • 27
0

try adding maxLines with the lines count you want

<TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:ellipsize="end"
            android:maxLines="4"
            android:text="hello\n hello\n hello\n hello hello hello hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/image" />
Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21
  • hey bro,this is my attempt 3,i tried to change maxLines=1 to maxLines=multiple lines, still not work – Kennen Jul 28 '20 at 02:37
0

Set property in java file

yourTextView.setEllipsize(TextUtils.TruncateAt.END);
yourTextView.setMaxLines(3);
yourTextView.setText("long text");
Dinesh
  • 1,410
  • 2
  • 16
  • 29
0

Please note that the ellipsis will only be shown if the text size exceeds the number of lines allowed or the text size width; otherwise, it will not be shown.

If what you want is that at the end of the text the ellipsis is always shown, you must create a method which concatenates the (text + "...").

something like this:

TextView textView = findViewById(R.id.textView);
    String valueText = textView.getText().toString();
    if (!valueText.isEmpty()) valueText = String.format("%s...", valueText);
    textView.setText(valueText);

The following example shows the ellipsis mentioned above, the first text exceeds the size of the text view and the second does not.

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="hello hello hello hello hello hdsafsfdfdel sdsdasdsadasdsadsadasdasdsadsaadssadsadsadsadsasadsadsadsadsadsadsaadsalo hello hello hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="hello hello hello hello hello hel sfdsafsafsfasfsafsafsahhhhhhhhhhhhhh"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

enter image description here

  • Hi @Johnathan Yesid,the text in xml code I post is definitely exceeded the length allowed,but it still not work.And I used this xml attribute in another project I was involved in and it works fine,so I just feel very confused and don't want to acheive this programmatically,anyway,thx for your help,i will give it a try if there is no other way out – Kennen Jul 28 '20 at 02:27