Reputation: 1842
I want to have a single lined TextView
to show up 3 dots at the end when the text is longer than the TextView
. I don't know why - but I don't get it.
I already wrapped my head around similar StackOverflow questions, but I ended up with no solution. Maybe someone has some useful hints.
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:textStyle="bold"
android:text="Full Name"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_width="wrap_content"
android:id="@+id/lName"
android:layout_gravity="center_vertical"
android:maxLines="1"
android:ellipsize="end"/>
</LinearLayout>
The LinearLayout
above is nested into 2 other LinearLayouts
. Maybe this is important to know. I already tried the attribute "singleLine
" too, but some say this is deprecated and it doesnt work anyway.
Upvotes: 36
Views: 59561
Reputation: 71
Okay this way worked for me.
I am using the constraint layout and forgot to add the constraint at the end after adding the constraints I can able to see the ellipsis at the end of the textview
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_10sdp"
android:text="asdasdnadnlaskldaklskdlasnkldsnkla"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
style="@style/TextStyle"
android:ellipsize="end"
android:maxLines="1"
android:textStyle="bold"
android:textColor="@color/black"
/>
Upvotes: 0
Reputation: 1
Use this code it will definitely work.
<TextView
android:id="@+id/sAddress"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="Adress"
android:textSize="14sp"/>
Upvotes: 0
Reputation: 40898
This doesn't work with me unless adding 'android:ems'
attribute along with the android:ellipsize
attribute
android:ellipsize="end"
android:singleLine="true"
android:ems="8"
Upvotes: 5
Reputation: 39
The ellipses did not appear while my text was selectable. I needed to disable selectable text (which is the default):
android:layout_width="0dp" (match constraint)
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textIsSelectable="false"
Upvotes: 3
Reputation: 7605
Add this in your xml for the TextView:
android:maxWidth="200dp"
android:maxLines="1"
As
android:singleLine="true"
is deprecated.
Upvotes: 24
Reputation: 5336
Add the following styles in your styles file (typically styles.xml
):
<style name="autoscroll">
<item name="android:singleLine">true</item>
<item name="android:ellipsize">marquee</item>
<item name="android:marqueeRepeatLimit">marquee_forever</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:scrollHorizontally">true</item>
</style>
Then add the style @style/autoscroll
to your TextView
:
<TextView android:id="@+id/lName"
style="@style/autoscroll" />
You can reuse your autoscroll feature easily when you want this way.
Upvotes: 56
Reputation: 9397
It works with singleLine="true"
but this attribute is now deprecated, use ellipsize and scrollHorizontally="true"
instead.
Upvotes: 1
Reputation: 1577
android:id="@+id/lName" android:layout_width="150dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:text="Avinljhakjhsajkhakjshda"
android:textSize="16sp"
Upvotes: 9