Search in sources :

Example 21 with ClickableSpan

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;
}
Also used : Layout(android.text.Layout) ClickableSpan(android.text.style.ClickableSpan)

Example 22 with ClickableSpan

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);
    }
}
Also used : Matcher(java.util.regex.Matcher) ClickableSpan(android.text.style.ClickableSpan)

Example 23 with ClickableSpan

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());
    }
}
Also used : Matcher(java.util.regex.Matcher) ClickableSpan(android.text.style.ClickableSpan)

Example 24 with ClickableSpan

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) {
            }
        }
    };
}
Also used : Context(android.content.Context) ClipboardManager(android.content.ClipboardManager) Intent(android.content.Intent) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View)

Example 25 with ClickableSpan

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());
}
Also used : URLSpan(android.text.style.URLSpan) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

ClickableSpan (android.text.style.ClickableSpan)62 View (android.view.View)25 Layout (android.text.Layout)20 TextPaint (android.text.TextPaint)19 TextView (android.widget.TextView)15 Spannable (android.text.Spannable)14 SpannableString (android.text.SpannableString)14 Paint (android.graphics.Paint)13 SpannableStringBuilder (android.text.SpannableStringBuilder)8 Intent (android.content.Intent)7 InputMethodManager (android.view.inputmethod.InputMethodManager)7 Context (android.content.Context)4 CustomTabsIntent (android.support.customtabs.CustomTabsIntent)4 StyleSpan (android.text.style.StyleSpan)4 DialogInterface (android.content.DialogInterface)3 AlertDialog (android.support.v7.app.AlertDialog)3 ImageView (android.widget.ImageView)3 SpanInfo (org.chromium.ui.text.SpanApplier.SpanInfo)3 PendingIntent (android.app.PendingIntent)2 Uri (android.net.Uri)2