3

I have a text view under card view I want to how three dots at the end of text view when text is long enough.

I have tried adding below lines in my text view but could not help me.

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

Screenshot

enter image description here

This is my layout below:

 <androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#fff"
    app:cardCornerRadius="3dp"
    app:contentPadding="3dp"
    app:cardUseCompatPadding="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/homeBookImage"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_marginEnd="232dp"
            android:layout_marginRight="232dp"
            android:scaleType="fitXY"
            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.0" />

         <TextView
            android:id="@+id/homeBookName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:text=""
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/homeBookImage"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            android:singleLine="true"
            android:maxLines="1"
            android:ellipsize="end"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

Someone please let me know how can I achieve desired layout. Any help would be appreciated.

THANKS

Digvijay
  • 2,887
  • 3
  • 36
  • 86

4 Answers4

2

You're using ConstraintLayout inside CardView and I can see that you've constrained the TextView properly but its width is wrap_content which what causing the issue. set it to 0dp and your android:ellipsize and android:singleLine attributes will work.

Try this:

<TextView
    ...
    android:layout_width="0dp"
    ... />
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
2
android:singleLine="true" or
android:maxLines="1"
android:ellipsize="end"  

Right along with this, you need to specify the width to match parent(alongside the weight as per your design). If the text content exceeds the length the ... will be shown.

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Abhijeet
  • 87
  • 3
1

You can check for your wanted length and do it from your code like this:

 if (yourText.length() > 30){ 
      textView = textView .substring(0, 30); //textView length is reduced to add ...
      something.setText(textView + "...");   //adding the ... at the end
 }
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
0

Give a fixed-width or match_parent

<TextView
       android:layout_width="200dp" // match_parent
       android:layout_height="wrap_content"
       android:singleLine="true"
       android:text="Your text"/>