android - Can I disable the scrolling in TextView when using LinkMovementMethod? - TagMerge
6Can I disable the scrolling in TextView when using LinkMovementMethod?Can I disable the scrolling in TextView when using LinkMovementMethod?

Can I disable the scrolling in TextView when using LinkMovementMethod?

Asked 1 years ago
18
6 answers

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());

Source: link

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

Source: link

0

simply using :

textview.setEnabled(false);

Source: link

0

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());
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
}
}
simply using :
textview.setEnabled(false);

Source: link

0

There are two ways of “linkifying” URLs in a TextView. First, as an XML attribute:
<TextView
  ...
  android:autoLink="phone|web" />
and second, programmatically:
TextView textView = (TextView) findViewById(R.id.text1);
Linkify.addLinks(textView, Linkify.PHONE_NUMBERS | LINKIFY.WEB_URLS);
In both the cases, the framework internally registers a LinkMovementMethod on the TextView that handles dispatching a ACTION_VIEW Intent when any link is clicked. This is why phone-numbers open in a dialer when clicked, web URLs open in a browser, map URLs open in Google Maps and so on. The source can be seen in URLSpan.class (line #63):
@Override
public void onClick(View widget) {
  Uri uri = Uri.parse(getURL());
  Context context = widget.getContext();
  Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
  try {
    context.startActivity(intent);
  }
  ...
}
Introducing BetterLinkMovementMethod, a.. uh.. better a version of LinkMovementMethod that solves all our problems. It’s designed to be a drop-in replacement for LinkMovementMethod:
TextView textView = (TextView) findViewById(R.id.text1);
textView.setMovementMethod(BetterLinkMovementMethod.newInstance());
Linkify.addLinks(textView, Linkify.PHONE_NUMBERS);
However, the easiest way to get started is by using one of its linkify() methods:
BetterLinkMovementMethod.linkify(Linkify.ALL, this)
    .setOnLinkClickListener((textView, url) -> {
      // Do something with the URL and return true to indicate that 
      // this URL was handled. Otherwise, return false to let Android
      // handle it.
      return true;
    })
    .setOnLinkLongClickListener((textView, url) -> {
      // Handle long-clicks.
      return true;
    });

Source: link

0

Try these lines of code in xml
android:isScrollContainer="false"
android:ellipsize="end"
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;
}

}

Source: link

Recent Questions on android

    Programming Languages