saket / Better-Link-Movement-Method

Attempts to improve how clickable links are detected, highlighted and handled in TextView
Apache License 2.0
780 stars 78 forks source link

Crash on link click if text is selectable #11

Closed pchupik closed 6 years ago

pchupik commented 6 years ago

In sample app make the textView text-selectable (e.g. by adding android:textIsSelectable="true")

  1. Select some text in the textView which contains clickableSpan
  2. Click on the clickableSpan crash.txt
    java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
    ...
    at me.saket.bettermovementmethod.BetterLinkMovementMethod.highlightUrl(BetterLinkMovementMethod.java:342)
    at me.saket.bettermovementmethod.BetterLinkMovementMethod.onTouchEvent(BetterLinkMovementMethod.java:222)
saket commented 6 years ago

Hey @pchupik, I'm busy traveling till 10th of Nov and so won't be able to look into this until then. :(

pchupik commented 6 years ago

Looks like I found a fix for this. In BetterLinkMovementMethod.highlightUrl( ) add check textView for selection and clear focus if it has one:

text.setSpan(new BackgroundColorSpan(textView.getHighlightColor()), spanStart, spanEnd, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
if (textView.hasSelection())
           textView.clearFocus();
textView.setText(text);
saket commented 6 years ago

Fixed in commit d0a7db1. Will release a new version soon. In the meanwhile, you can get a build off HEAD using jitpack.io.

saket commented 6 years ago

Fixed in 2.1.0: https://github.com/Saketme/Better-Link-Movement-Method/releases/tag/v2.1.0