A screen in my app will potentially post really long strings into a TextView
. For this scenario, I have android:ellipsize="marquee"
set so the text will marquee across the TextView.
However, I've decided I also want this text to be selectable (android:textIsSelectable="true"
). In most cases, this is no problem. The text is smaller than the TextView and the user can just select it. However, if I have the textIsSelectable
attribute and if the text is bigger than the TextView, the text will pick up an ellipse instead of being the full string. It will still marquee, but it no longer displays the full text. It cuts it off and displays an ellipse.
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:ellipsize="marquee"
android:focusable="true"
android:gravity="center_vertical"
android:singleLine="true"
android:textIsSelectable="true">
Is there a way to have the text selectable and still maintain the entire string in the marquee (no ellipse)?
Can't be sure if this is a bug.
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:ellipsize="start"
android:focusable="true"
android:gravity="center_vertical"
android:singleLine="true"
android:textIsSelectable="true"/>
Note that we're setting android:ellipsize="start"
in xml - more on this later.
mTextView = (TextView) findViewById(R.id.tv);
mTextView.post(new Runnable() {
@Override
public void run() {
mTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
mTextView.setSelected(true);
}
});
setEllipsize(TruncateAt)
checks whether the current ellipsize
value is the same as the supplied one. To get around this, we supply android:ellipsize="start"
in xml. This way, the TextView
has no problem accepting TextUtils.TruncateAt.MARQUEE
later on.
Now, even though this works, I will suggest you don't do this. You'll be able to see why - once you try this code. It seems textIsSelectable
is not supposed to be used with marquee
- the selection handles don't move along with the text.
All in all, it looks extremely sketchy.
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