use of android.text.style.ClickableSpan in project Rocket.Chat.Android by RocketChat.
the class LinkMovementMethodCompat method onTouchEvent.
// http://stackoverflow.com/a/30572151/2104686
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
int eventX = (int) event.getX();
int eventY = (int) event.getY();
eventX -= widget.getTotalPaddingLeft();
eventY -= widget.getTotalPaddingTop();
eventX += widget.getScrollX();
eventY += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(eventY);
int off = layout.getOffsetForHorizontal(line, eventX);
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(widget);
} else {
Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
}
return true;
}
}
return false;
}
use of android.text.style.ClickableSpan in project Rocket.Chat.Android by RocketChat.
the class MarkDown method highlightLink1.
private static void highlightLink1(SpannableString inputText) {
final Matcher matcher = LINK_PATTERN.matcher(inputText);
while (matcher.find()) {
ClickableSpan span = createLinkSpan(matcher.group(2));
setSpan(span, inputText, matcher.start(), matcher.end(), 1, matcher.group(2).length() + 3);
}
}
use of android.text.style.ClickableSpan in project Rocket.Chat.Android by RocketChat.
the class MarkDown method highlightLink2.
private static void highlightLink2(SpannableString inputText) {
Matcher matcher = LINK_PATTERN2.matcher(inputText);
while (matcher.find()) {
ClickableSpan span = createLinkSpan(matcher.group(2));
setSpan(span, inputText, matcher.start(), matcher.end(), matcher.group(1).length() + matcher.group(2).length() + 1, matcher.group(4).length());
}
}
use of android.text.style.ClickableSpan in project Rocket.Chat.Android by RocketChat.
the class MarkDown method createLinkSpan.
/*package*/
static ClickableSpan createLinkSpan(final String url) {
return new ClickableSpan() {
@Override
public void onClick(View view) {
final Context context = view.getContext();
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return;
} catch (Exception exception) {
}
try {
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText("linkURL", url));
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
} catch (Exception exception) {
}
}
};
}
use of android.text.style.ClickableSpan in project Tusky by Vavassor.
the class LinkHelper method setClickableText.
public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions, boolean useCustomTabs, final LinkListener listener) {
SpannableStringBuilder builder = new SpannableStringBuilder(content);
URLSpan[] urlSpans = content.getSpans(0, content.length(), URLSpan.class);
for (URLSpan span : urlSpans) {
int start = builder.getSpanStart(span);
int end = builder.getSpanEnd(span);
int flags = builder.getSpanFlags(span);
CharSequence text = builder.subSequence(start, end);
if (text.charAt(0) == '#') {
final String tag = text.subSequence(1, text.length()).toString();
ClickableSpan newSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
listener.onViewTag(tag);
}
};
builder.removeSpan(span);
builder.setSpan(newSpan, start, end, flags);
} else if (text.charAt(0) == '@' && mentions != null) {
final String accountUsername = text.subSequence(1, text.length()).toString();
String id = null;
for (Status.Mention mention : mentions) {
if (mention.localUsername.equals(accountUsername)) {
id = mention.id;
}
}
if (id != null) {
final String accountId = id;
ClickableSpan newSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
listener.onViewAccount(accountId);
}
};
builder.removeSpan(span);
builder.setSpan(newSpan, start, end, flags);
}
} else if (useCustomTabs) {
ClickableSpan newSpan = new CustomTabURLSpan(span.getURL());
builder.removeSpan(span);
builder.setSpan(newSpan, start, end, flags);
}
}
view.setText(builder);
view.setLinksClickable(true);
view.setMovementMethod(LinkMovementMethod.getInstance());
}
Aggregations