Can I disable scrolling in a TextView when using LinkMovementMethod?

I use clickable span in textView to allow clicks only part of the text. It works fine, except that the textView scrolls and I don’t want something. This is because I use LinkMovementMethod, which scrolls if necessary. Is there a way to undo scrolling?

SpannableString ss = "My text [click area] end." ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { // My click action } }; // Set the span String fromString = "text"; int startClickPos = ss.toString().indexOf(fromString)+fromString.length()+1; int endCickPos=startClickPos+ 12; ss.setSpan(clickableSpan, startClickPos, endCickPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(ss); textView.setMovementMethod(LinkMovementMethod.getInstance()); 
+5
android textview scroll
source share
3 answers

I use this code to disable scrolling for a TextView using clickableSpan.

 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 texture as a touch listener: -

 textview.setOnTouchListener(new LinkMovementMethodOverride()); 
+11
source share

just using:

 textview.setEnabled(false); 
+1
source share
  • 100% works for me, there is no need to enable = false in adnroid, you can use with autolink and without scrolling.

     public class PreventScrollTextView extends TextView { public PreventScrollTextView(Context context) { super(context); } public PreventScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); } public PreventScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public PreventScrollTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void scrollTo(int x, int y) { //do nothing } } 
0
source share

All Articles