Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting textIsSelectable on TextView with marquee ellipsize adds ellipse

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)?

like image 726
Andrew Avatar asked Nov 23 '15 13:11

Andrew


1 Answers

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.

like image 177
Vikram Avatar answered Oct 25 '22 12:10

Vikram