Digvijay
Digvijay

Reputation: 3281

How to show three dots at the end of text view

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

Upvotes: 3

Views: 4123

Answers (4)

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

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
 }

Upvotes: 1

Abhijeet
Abhijeet

Reputation: 87

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.

Upvotes: 2

Abdul Mohseen
Abdul Mohseen

Reputation: 1

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"/>

Upvotes: 0

Birju Vachhani
Birju Vachhani

Reputation: 6373

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"
    ... />

Upvotes: 2

Related Questions