0

I am trying to create a ListView with linkes in it's view's. Just like the twitter app. right now that's whats Im doing: I set in the TextView android:autoLink="all".

private static class TweetViewHolder {
    public LinkifiedTextView text;
    public TextView time;
    public ImageView authorImage;
}

holder.text.setText(post.getTweet().getText());
Pattern pattern = Pattern.compile("@+[a-zA-Z_]+");
Linkify.addLinks(holder.text, pattern, "http://www.twitter.com/", null, myTransformFilter);
pattern = Pattern.compile("#+[a-zA-Z_]+");
Linkify.addLinks(holder.text, pattern, "http://www.twitter.com/", null, myTransformFilter);

TransformFilter myTransformFilter = new TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
 return url.substring(1); //remove the $ sign
}
};

This is my "LinkifiedTextView" code:

@Override
public boolean onTouchEvent(MotionEvent event) {
  TextView widget = (TextView) this;
  Object text = widget.getText();
  if (text instanceof Spanned) {
      Spannable buffer = (Spannable) 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.setSelection(buffer,
                          buffer.getSpanStart(link[0]),
                          buffer.getSpanEnd(link[0]));

              }
              return true;
          }
      }

  }

  return false;

}

Right now the code is crushing on "Spannable buffer = (Spannable) text;" Why is that and how can I change things to make it work? Thanks!

roiberg
  • 13,629
  • 12
  • 60
  • 91

1 Answers1

1

Because not all Spanneds are Spannables. Spannable is a subclass of Spanned, but not the only one. The text object is some other subclass.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Make the line Spanned buffer = (Spanned) text; instead – Gabe Sechan Jul 01 '13 at 16:36
  • But than it will crush on Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); – roiberg Jul 01 '13 at 16:39
  • It shouldn't, spanned has those functions. If it does, what's the error? – Gabe Sechan Jul 01 '13 at 16:41
  • This is the error: the method setSelection(Spannable, int, int) in the type Selection is not applicable for the arguments (Spanned, int, int) – roiberg Jul 01 '13 at 16:42
  • Ok, so you may need to solve it the other way- instead run this code only if the text is instanceof Spannable. In either case, you can't check if its a Spanned then assume it will be a Spannable. – Gabe Sechan Jul 01 '13 at 16:51
  • I tried that, now it's not "catching" the linkes that I add with "Linkify.addLinks", It's only catches the "android:autoLink="all". Sorry for this discussion but I can't get it to work. Why is the Linkify.addLinks not spannable or how can I make it one? – roiberg Jul 01 '13 at 16:56