Is There A Standard Way To Have Hrefs And Telephone Numbers Clickable In A Textview?
I already know how to use TextView to automatically create clickable links for web addresses, phone numbers, etc.. My question is when you have HTML with hrefs in it as well as pho
Solution 1:
You may use cutom LinkMovementMethod
implementation. Like this:
public class CustomLinkMovementMethod extends LinkMovementMethod {
private static CustomLinkMovementMethod linkMovementMethod = new BayerLinkMovementMethod();
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
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);
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length != 0) {
String url = link[0].getURL();
if (url.startsWith("http://") || url.startsWith("https://")) {
//do anything you want
} else if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
widget.getContext().startActivity(intent);
return true;
} else if (url.startsWith("mailto:")) {
Intent intent = new Intent(Intent.ACTION_SENDTO,
Uri.parse(url));
widget.getContext().startActivity(intent);
return true;
}
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
public static MovementMethod getInstance() {
return linkMovementMethod;
}
}
Post a Comment for "Is There A Standard Way To Have Hrefs And Telephone Numbers Clickable In A Textview?"