0

I have a TextView which should display a maximum of 2 lines of text and the rest should be ellipsized(if that's the right term)

Here's what I'm doing

android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"

But the text isn't getting cut off at the end as expected.

Here's my code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <TextView
            android:id="@+id/note_priority"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:paddingLeft="10dp"
            android:text="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/note_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_toStartOf="@+id/note_priority"
            android:singleLine="true"
            android:text="TitleTitleTitleTitleTitleTitleTitleTitleTitleTitle"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/note_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/note_title"
            android:ellipsize="marquee"
            android:maxLines="2"
            android:singleLine="false"
            android:text="DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

EDIT: I have already tried using end and marquee both as the value for android:ellipsize

4
  • In your xml, you have ellipsize set to marquee. Change it to end. Like this, android:ellipsize="end" Commented Mar 12, 2020 at 5:37
  • In your code, unused android:ellipsize="end" ... haha..
    – Genesis
    Commented Mar 12, 2020 at 6:17
  • @AbdullahRiaz, I tried that already, but it doesn't work
    – dkhost07
    Commented Mar 12, 2020 at 6:20
  • @Genesis, I've updated the question
    – dkhost07
    Commented Mar 12, 2020 at 6:23

1 Answer 1

1

I tried to reproduce it with your code.

Is this not the behavior you want?

From what I understand, it seems like the results you want and the results I tested.

And, singleLine is not necessary because the default value is false.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <TextView
            android:id="@+id/note_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="DescriptionDescriptionDescriptionDescriptionDescriptionDescaefawfeawfewafewaefawefasefsaefriptionDescriptionDescriptionDescriptionDescriptionDescription" />

    </RelativeLayout>
</androidx.cardview.widget.CardView>

Preview

Preview

2
  • And, singleLine is deprecated.
    – Genesis
    Commented Mar 12, 2020 at 6:33
  • No idea why this doesn't get shown as the pic you posted in my design preview. However, it does work when I run the app. So thank you.
    – dkhost07
    Commented Mar 12, 2020 at 10:57

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.