I am trying to create a textview so that if the number of characters goes above 22, an ellipses " . . ." would appear. However this does not work with maxLength.
<TextView
android:id="@+id/vh_fragnotifications_postedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="8dp"
android:text="zz12345678901234567890aaaaaaaa"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_body"
app:layout_constraintLeft_toLeftOf="@+id/vh_fragnotifications_body"
app:layout_constraintRight_toLeftOf="@+id/vh_fragnotifications_dateposted"
android:maxLines="1"
android:ellipsize="end"
android:maxLength="22"/>
How can I get the ellipses to appear while keeping the max digits of 22?
Here is the full layout:___________________________________________________
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorWhite">
<ImageView
android:id="@+id/vh_fragnotifications_imagepreview"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/vh_fragnotifications_body"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0"/>
<TextView
android:id="@+id/vh_fragnotifications_body"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
android:textColor="@color/colorBlackFont"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintLeft_toRightOf="@+id/vh_fragnotifications_imagepreview"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/vh_fragnotifications_imagepreview"/>
<TextView
android:id="@+id/vh_fragnotifications_postedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="8dp"
android:text="zz12345678901234567890aaa"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_body"
app:layout_constraintLeft_toLeftOf="@+id/vh_fragnotifications_body"
app:layout_constraintRight_toLeftOf="@+id/vh_fragnotifications_dateposted"
android:ellipsize="end"
android:maxLength="22"
android:maxLines="1"/>
<TextView
android:id="@+id/vh_fragnotifications_dateposted"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="@+id/vh_fragnotifications_body"
android:layout_marginTop="8dp"
android:text=" • 18 hours ago"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_body"
app:layout_constraintLeft_toRightOf="@+id/vh_fragnotifications_postedby"
app:layout_constraintRight_toRightOf="@+id/vh_fragnotifications_body"
android:maxLines="1"
android:ellipsize="end" />
<View
android:id="@+id/vh_fragnotifications_divider"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:background="?android:attr/dividerVertical"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="@+id/vh_fragnotifications_body"
app:layout_constraintRight_toRightOf="@+id/vh_fragnotifications_body"
app:layout_constraintTop_toBottomOf="@+id/vh_fragnotifications_dateposted"/>
</androidx.constraintlayout.widget.ConstraintLayout>
you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.
Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.
You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!
Here we have used android:ellipsize=”marquee” to add a marquee to our text and android:singleLine=”true” so that our text will show only in one line.
Try this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="8dp"
android:text="zz12345678901234567890aaaaaaaa"
android:maxLines="1"
android:ellipsize="end"
android:maxEms="11"
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With