I have a TextView
that I want to limit characters of it. Actually, I can do this but the thing that I\'m looking for is how to add three dots (...) at the end o
The following is what I learned by playing around with various options for forcing a TextView
to a single line (with and without the three dots).
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="one two three four five six seven eight nine ten" />
This just forces the text to one line. Any extra text is hidden.
Related:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="one two three four five six seven eight nine ten" />
This cuts off the text that doesn't fit but lets users know that the text has been truncated by adding an ellipsis (the three dots).
Related:
<TextView
android:id="@+id/MarqueeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="one two three four five six seven eight nine ten" />
This makes the text scroll automatically across the TextView. Note that sometimes it needs to be set in code:
textView.setSelected(true);
Supposedly android:maxLines="1"
and android:singleLine="true"
should do basically the same thing and since singleLine is apparently deprecated I would prefer not to use it, but when I take it out, the marquee doesn't scroll anymore. Taking maxLines
out doesn't affect it, though.
Related:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="one two three four five six seven eight nine ten" />
</HorizontalScrollView>
This allows the user to manually scroll to see the whole line of text.
Use
android:singleLine="true"
android:maxLines="1"
app:layout_constrainedWidth="true"
It's how my full TextView
looks:
<TextView
android:id="@+id/message_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:maxLines="1"
android:singleLine="true"
android:text="NAME PLACEHOLDER MORE Text"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@id/message_check_sign"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="@id/img_chat_contact"
app:layout_constraintTop_toTopOf="@id/img_chat_contact" />
Deprecated:
Add one more property android:singleLine="true"
in your Textview
Updated:
android:ellipsize="end"
android:maxLines="1"
You need to add following lines into your layout for the textview
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
Hope this works for you.
code:
TextView your_text_view = (TextView) findViewById(R.id.your_id_textview);
your_text_view.setEllipsize(TextUtils.TruncateAt.END);
xml:
android:maxLines = "5"
e.g.
In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know the mysteries of the kingdom of heaven, but to them it has not been given.
Output: In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know...
Try this property of TextView in your layout file..
android:ellipsize="end"
android:maxLines="1"