9

I'm using clickable span in a textView to enable only part of the text to be clickable. It works fine except that the textView is scrolling down and that's something I don't want. It happens because I use LinkMovementMethod that scrolls if needed. Is there anyway to cancel the 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());
user1787773
  • 888
  • 1
  • 11
  • 19

4 Answers4

19

I am using this code to disable scrolling for TextView with 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 textview as touch listener: -

textview.setOnTouchListener(new LinkMovementMethodOverride());
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Benny Khoo
  • 725
  • 6
  • 15
  • 1
    It works. By the way, this code is identical to the code in `LinkMovementMethod.onTouchEvent()`, which is the part that actually handles the link clicks. – Pin Aug 07 '15 at 13:50
3
  • 100% working for me, no need to enable= false in adnroid, you can use with autolink and without scroll.

    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
    }
    }
    
2

This class is defined as:

public class LinkMovementMethod extends ScrollingMovementMethod {

So, by using LinkMovementMethod, you've explicitly asked it to scroll.

If you want to have clickable links but not scroll, simply duplicate the LinkMovementMethod class with just one change:

public class MyLinkMovementMethod extends BaseMovementMethod {
     //copy all the code from LinkMovementMethod
}

And use it instead.

Agent_L
  • 4,960
  • 28
  • 30
  • It's not working because you don't have in this class a code that recognise the spans and make the click callback... – jazzyjester Apr 28 '22 at 10:56
  • @jazzyjester What's not working? The code is same as in `LinkMovementMethod`, only the base class differs. I've made edits, is it clearer now? – Agent_L Apr 28 '22 at 12:59
  • 1
    Oh you mean to leave all the code and make only this change by extending different class...can work But you will have a lot of code not in use... The solution in th bottom with the onTouch worked for me. – jazzyjester Apr 29 '22 at 13:12
  • @jazzyjester Well, depending on how you look at it. It's certainly more code in your project. But if you look at how much code is actually run: it's running the `ScrollingMovementMethod` and also running an onTouch listener that blocks the former. From that point of view my solution runs no scrolling code at all rather than 2 pieces meant to cancel each other. It's also less fragile because you set one thing instead of 2 things in 2 different places. It's easy to forget about adding the listener. – Agent_L Apr 29 '22 at 13:26
-1

simply using :

textview.setEnabled(false);
Rocel
  • 1,029
  • 1
  • 7
  • 22