Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textIsSelectable is making TextView scroll

<TextView
    android:id="@+id/myText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="5"
    android:padding="16dip"
    android:textIsSelectable="true"
    android:textSize="16sp"/>

If android:textIsSelectable is not present, the TextView has a maxLines of 5 and you cannot get the TextView to scroll, even if there is additional text that has run off the view.

However, with the addition of android:textIsSelectable, the TextView will still have a maximum line count of 5, but you can make it scroll by tapping the bottom border of the TextView or selecting some text and dragging the text selector controls.

Is there a way to prevent the TextView from being scrollable when android:textIsSelectable is present?

like image 319
Andrew Avatar asked Oct 30 '22 12:10

Andrew


2 Answers

Well, you might not understand what android:textIsSelectable is already doing...

According to TextView documentation (you can find it here):

To allow users to copy some or all of the TextView's value and paste it somewhere else, set the XML attribute android:textIsSelectable to true or call setTextIsSelectable(true).

The textIsSelectable flag allows users to make selection gestures in the TextView, which in turn triggers the system's built-in copy/paste controls.

It seems to be that scrolling is one of the main features of isSelectable flag, but ...

According to question, here is a post from: How do I completely prevent a TextView from scrolling?

So I did a little research and I don't think it's as simple as just disabling scrolling, but there are a few things you can do/try.

The first is setEnabled(false) but this will disable links and alter the text color.

The second, which I suggest trying, is using the scrollTo(int x, int y) method. Just scrollTo(0,0) after setting the text of the TextView, my guess is the large text is the only thing causing the scrolling so this should be able to take care of it.

The third answer I found that you can try is a bit more complicated and not exactly your question but it may work for you can be found here.

public class LinkMovementMethodOverride implements View.OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {
    TextView widget = (TextView) v;
    Object text = widget.getText();
    if (text instanceof Spanned) {
        Spanned buffer = (Spanned) text;

        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = buffer.getSpans(off, off,
                    ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);
                } else if (action == MotionEvent.ACTION_DOWN) {                             
                    // Selection only works on Spannable text. In our case setSelection doesn't work on spanned text
                    //Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                }
                return true;
            }
        }

    }

    return false;
}

}

"After that apply it to the target textview as touch listener: -

textview.setOnTouchListener(new LinkMovementMethodOverride());"

You can also simply try to put this lines to your TextView attributes:

android:isScrollContainer="false"
android:ellipsize="end"

Hope it help

like image 92
piotrek1543 Avatar answered Nov 15 '22 05:11

piotrek1543


You can create your custom non scrollable TextView

public class NonScrollableTextView extends TextView {
    public NonScrollableTextView(Context context) {
        super(context);
    }

    public NonScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NonScrollableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void scrollTo(int x, int y) {
        //nop
    }
}
like image 27
lukjar Avatar answered Nov 15 '22 07:11

lukjar